MCPTools within AgentOS
Learn how to use MCPTools in the Agents, Teams and Workflows within your AgentOS
The Model Context Protocol (MCP) enables Agents to interact with external systems through a standardized interface.
You can give your Agents access to MCP tools using the MCPTools class. Read more about using MCP tools here.
Your MCPTools will work normally within AgentOS. Their lifecycle is automatically handled, you don't need to handle their connection and disconnection.
If you are using MCPTools within AgentOS, you should not use reload=True when serving your AgentOS.
This can break the MCP connection during the FastAPI lifecycle.
Example
1from kern.agent import Agent2from kern.os import AgentOS3from kern.tools.mcp import MCPTools45# Create MCPTools instance6mcp_tools = MCPTools(7 transport="streamable-http",8 url="https://kern.ndx.rocks/mcp"9)1011# Create MCP-enabled agent12agent = Agent(13 id="kern-agent",14 name="Kern Agent",15 tools=[mcp_tools],16)1718# AgentOS manages MCP lifespan19agent_os = AgentOS(20 description="AgentOS with MCP Tools",21 agents=[agent],22)2324app = agent_os.get_app()2526if __name__ == "__main__":27 # Don't use reload=True with MCP tools to avoid lifespan issues28 agent_os.serve(app="mcp_tools_example:app")Refreshing the connection to MCP servers is not automatically handled. You can use refresh_connection to manually refresh a connection.
See here for a full example.