Skip to main content
Room services are scoped to a specific room and persist across sessions. They’re ideal when users need to customize services for example, providing their own system prompt or API key. This example shows how to create a simple chatbot that lets users provide a custom system prompt when they add it to their room. We’ll create a containerized MeshAgent-native service using a ServiceTemplate so users can configure the system prompt when they add it to their room. To learn more about project vs room services and understand when to use each see intro to services and containers.

Example

Let’s create a simple chatbot that allows users to give it a system prompt when deploying. To deploy this we’ll create three files:
  • main.py: A MeshAgent-native chatbot service using ServiceHost
  • Dockerfile: Packages the service as a container
  • meshagent.yaml: A ServiceTemplateSpec that collects a system prompt variable from users when they add the service to their room

main.py

import asyncio
from meshagent.otel import otel_config
from meshagent.api.services import ServiceHost
from meshagent.agents.chat import ChatBot
from meshagent.openai import OpenAIResponsesAdapter

# Enable OpenTelemetry logging and tracing for the agent
otel_config(service_name="chatbot")

# Create a service host
service = ServiceHost()  # optional to pass a port, MeshAgent automatically assigns an available one if none provideds

# Register an agent at a specific path
@service.path(path="/chat", identity="chatbot")
class SimpleChatbot(ChatBot):
    def __init__(self):
        super().__init__(
            name="chatbot",
            title="Simple Chatbot",
            description="A helpful chatbot for room participants",
            rules=[
                "Always respond to the user first then include a fun fact at the end of your response."
            ],
            llm_adapter=OpenAIResponsesAdapter(),
        )

# Start the service
asyncio.run(service.run())

Dockerfile

Dockerfile
FROM meshagent/python-sdk-slim:latest

COPY . /src

WORKDIR /src

ENTRYPOINT [ "python3", "main.py" ]

meshagent.yaml

yaml
kind: ServiceTemplate
version: v1
metadata:
  name: chatbot
  description: "A chatbot that takes a system prompt at runtime"                 
agents:
  - name: chatbot
    description: "Room-scoped conversational agent"
    annotations:
      meshagent.agent.type: "ChatBot"
ports:
- num: "*"                    # automatically assign an available MESHAGENT_PORT for the agent to run on 
  type: http
  liveness: "/"               # ensure the service is alive before connecting to the room
  endpoints:
  - path: /agent              # service path to call and run the agent on
    meshagent:
      identity: chatbot         # name of the agent as it shows up in the Room
container:
  image: "myrepository/chatbot-image:1"
  command: python3 main.py      # command executed inside the container
  environment:                  # passes variables into the container
    - name: SYSTEM_PROMPT
      value: "{system_prompt}"
variables:                    # exposed in MeshAgent Studio UI as form fields when someone launches the service (parameters collected when launching service)
    - name: "system_prompt"
      description: "Custom instructions for the agent to follow"

Build and Push the Container

Build and push your container to a registry:
bash
docker buildx build . \
  -t "myrepository/chatbot-image:1" \
  --platform linux/amd64 \
  --push
Note: We recommend docker buildx because it supports cross-platform builds, pushes directly to your registry in one step, and enables optional layer caching for much faster CI builds.

Deploy the Room Specific Service

From the terminal in the directory with your yaml file run:
bash
meshagent setup # authenticate to MeshAgent if not already
meshagent service create --file meshagent.yaml --room=myroom
You can also deploy the service to a specific room through MeshAgent Studio by selecting the room menu, selecting Service, then New Service, and filling in the details. For more details on deployment see the deploying services documentation.

Use the Service

  1. Open your room in MeshAgent Studio
  2. You’ll be prompted to provide a system prompt for the chatbot
  3. Once provided the service will start and the chatbot will appear in the participants tab

Verify the Service is Running

From the Developer Console at the bottom half of the room UI
  • Check the Containers tab, you’ll see your container with MeshAgent as the starter
  • Check the Logs tab for startup messages from your service
Now that your service is deployed try it out in MeshAgent Studio. Enter the room you deployed the service to, you will be prompted to supply a system prompt for the agent, once you do the service will start and you’ll see the chatbot in the participants tab. Room services are managed by MeshAgent: If you update the service configuration during the session, MeshAgent automatically updates the running service. If you delete the room service, it’s removed from the room. Whatever room services you have saved will match what’s running - MeshAgent keeps them in sync. Containers run on demand, and will not automatically restart the next time you go into the room. If you want a container with your configuration to save and start automatically the next time the room starts, you will need to deploy a project wide service to manage the containers. Otherwise you can start containers as needed through the CLI/API. For more information on room and project services see: