ServiceHost Basics
MeshAgent’sServiceHost exposes your agents or tools as HTTP webhook endpoints. When you create a ServiceHost, it starts an HTTP server that listens for incoming webhook requests from MeshAgent. When a webhook call arrives, the ServiceHost automatically spins up your agent or tool, connects it to the requested room, and manages its lifecycle until the session ends.
Let’s define a simple chatbot service:
Note: MeshAgent automatically assigns an available port to your service. You can set a specific port by passing theMESHAGENT_PORTenvironment variable or passing a port to theServiceHostconstructor. For example,host = ServiceHost(port=9000)orhost = ServiceHost(port=int(os.getenv("MESHAGENT_PORT", "9001"))).
Run Locally with the CLI
Save the code above asmain.py and run:
- Starts your
ServiceHostHTTP server locally - Discovers all
@host.pathendpoints automatically - Calls each endpoint into the specified room using the declared identity
myroom, and you will see the chatbot participant available for interaction.
Iterate Quickly
- Implement your agent or tool and register endpoints with
ServiceHost. - Run
meshagent service run "main.py" --room=myroomto connect it to a test room. - Interact via MeshAgent Studio, adjust code, and restart the CLI command as needed.
- Add more endpoints to the same
ServiceHostif you want to bundle related agents/tools together.
Working with multiple agents or tools in a single service
You can register multiple agents or tools in a singleServiceHost using different paths. This makes it easy to bundle related agents and tools together into a single deployable service.
Python
meshagent service run "main.py" --room=myroom will automatically detect and call all three endpoints into the room with their respective identities.
From Local Development to Deployment
In this tutorial we covered how to useServiceHost and the MeshAgent CLI, (e.g.,meshagent service run main.py --room=myroom) to test your service before containerizing it.
Once your service works locally here’s the path to deployment:
- Package your code: Create a Dockerfile with the required libraries. MeshAgent provides a variety of base images for your use.
- Author a manifest: Manifests differ for project and room services. Use
kind: Servicewhen deploying a project service orkind: ServiceTemplatefor room services. - Push to a registry: Make sure your image is available for MeshAgent to pull and run the container for the image.
- *Deploy: Use the MeshAgent Studio UI or the CLI to configure, deploy, and monitor services.
Next Steps
- Intro to services and containers: Understand deployment options
- Packaging Services: Package your service so it’s ready to deploy
- Deploying Services: Deploy your service to the project or a specific room
- Run a service as a container via the Containers API
- Create any required secure credentials before deployment via secrets & registry access.