Python
Explore usage of the Serve module in Python
The Serve module enables developers to expose components built with the BeeAI Framework through a server to external clients.
Out of the box, we provide implementations for prominent protocols such as A2A and MCP, allowing you to quickly serve existing functionalities.
You can also create your own custom adapter if needed.
The following table lists the currently supported providers:
| Name | Installation |
|---|---|
| A2A | pip install beeai-framework[a2a] |
| Agent Stack | pip install beeai-framework[agentstack] |
| MCP | pip install beeai-framework[mcp] |
| ACP (Zed editor, stdio) | pip install beeai-framework[acp-zed] |
| IBM watsonx Orchestrate | pip install beeai-framework |
| OpenAI Chat Completion API | pip install beeai-framework |
| OpenAI Responses API | pip install beeai-framework |
Once you initiate the appropriate server class (eg: AgentStackServer, OpenAIServer, …) you can do the following.
register method).deregister method).members property).serve method / aserve method).You can typically register one of the following:
If the given instance is not supported, the register method will raise an exception.
Nevertheless, you can easily register a custom factory to make it supported.
If the given server doesn’t support (or you just want to override the conversion is done) you can register a custom factory function which takes the instance of a given type and converts it into an instance that the server knows how to work with.
The following example showcases how we can add a support for PromptTemplate class (which exists in BeeAI Framework) so that it gets exposed as a Prompt in the MCP Server.
from typing import Any
from mcp.server.fastmcp.prompts.base import Prompt as MCPPromptfrom mcp.server.fastmcp.prompts.base import PromptArgumentfrom pydantic import BaseModel
from beeai_framework.adapters.mcp import MCPServer, MCPServerConfigfrom beeai_framework.template import PromptTemplate
def add_prompt_template_factory() -> None: def factory(instance: PromptTemplate[Any]) -> MCPPrompt: return MCPPrompt( name=instance.name, title=instance.name, description=instance.description, arguments=[ PromptArgument( name=k, description=v.description, required=v.default is None and v.default_factory is None ) for k, v in instance.input_schema.model_fields.items() ], fn=lambda **kwargs: instance.render(kwargs), )
MCPServer.register_factory(PromptTemplate, factory, override=True)
def run_server() -> None: class GreetingTemplateModel(BaseModel): name: str
my_template = PromptTemplate( template="Hello {name}", schema=GreetingTemplateModel, )
server = MCPServer(config=MCPServerConfig(transport="streamable-http")) server.register(my_template) server.serve()
if __name__ == "__main__": add_prompt_template_factory() run_server()