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.

Note

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 Agent
2from kern.os import AgentOS
3from kern.tools.mcp import MCPTools
4
5# Create MCPTools instance
6mcp_tools = MCPTools(
7 transport="streamable-http",
8 url="https://kern.ndx.rocks/mcp"
9)
10
11# Create MCP-enabled agent
12agent = Agent(
13 id="kern-agent",
14 name="Kern Agent",
15 tools=[mcp_tools],
16)
17
18# AgentOS manages MCP lifespan
19agent_os = AgentOS(
20 description="AgentOS with MCP Tools",
21 agents=[agent],
22)
23
24app = agent_os.get_app()
25
26if __name__ == "__main__":
27 # Don't use reload=True with MCP tools to avoid lifespan issues
28 agent_os.serve(app="mcp_tools_example:app")
Note

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.