mcp-tools-server.mdx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ---
  2. title: "MCP Tools Server"
  3. description: "Serving your tools as MCP (Model Context Protocol) endpoints."
  4. icon: "server"
  5. ---
  6. Agency Swarm supports serving your tools as production-ready MCP (Model Context Protocol) endpoints.
  7. This enables AI models to interact with your tools remotely over HTTP, making them accessible to any MCP-compatible client or AI system.
  8. ## Setting Up MCP Server
  9. To expose tools as a standalone MCP endpoint, use the `run_mcp()` function from the integrations module.
  10. This will create an MCP server that will serve provided tools over the streamable HTTP protocol.
  11. ### Example: Serving Individual Tools
  12. ```python
  13. from agency_swarm import function_tool, run_mcp
  14. from pydantic import BaseModel, Field
  15. class InputArgs(BaseModel):
  16. input_field: str = Field(..., description="Test input for the tool")
  17. @function_tool
  18. async def echo_function(args: InputArgs) -> str:
  19. """Returns a unique id"""
  20. return f"Tool called with input: {args.input_field}"
  21. # Start MCP server with individual tools.
  22. # This will setup a streamable-http server by default on port 8000
  23. run_mcp(tools=[echo_function])
  24. ```
  25. ### Configuration Options
  26. The `run_mcp()` function accepts several configuration parameters:
  27. ```python
  28. run_mcp(
  29. tools=[ExampleTool, TestTool], # List of Function tools or BaseTools
  30. host="0.0.0.0", # Host to bind server to
  31. port=8000, # Port to bind server to
  32. app_token_env="APP_TOKEN", # Environment variable for auth token
  33. server_name="mcp-tools-server", # MCP server identifier
  34. return_app=False, # Return app instead of running server
  35. transport="streamable-http" # Preferred mcp protocol to use
  36. )
  37. ```
  38. ### Authentication
  39. Authentication is controlled via environment variables:
  40. ```python
  41. import os
  42. os.environ["APP_TOKEN"] = "your-secret-token" # Or set in .env file
  43. ```
  44. If no `APP_TOKEN` is set, **authentication will be disabled** and the server will accept all requests.
  45. ---
  46. ## MCP Client Usage
  47. <Tabs>
  48. <Tab title="Local MCP Server">
  49. This example shows how to use a local MCP server with stdio transport:
  50. ```python
  51. import asyncio
  52. from agents.mcp.server import MCPServerStdio, MCPServerStdioParams
  53. from agency_swarm import Agency, Agent
  54. # Set up local MCP server using stdio transport
  55. stdio_server = MCPServerStdio(
  56. MCPServerStdioParams(
  57. command="python", # or "npx" if available
  58. # Path to your MCP server script or npx arguments
  59. args=["./path/to/your/mcp_server.py"]
  60. ),
  61. cache_tools_list=True
  62. )
  63. # Create agent with local MCP server
  64. mcp_agent = Agent(
  65. name="LocalMCPAgent",
  66. mcp_servers=[stdio_server],
  67. )
  68. agency = Agency(mcp_agent)
  69. async def run_local_example():
  70. await stdio_server.connect()
  71. response = await agency.get_response("List all mcp tools")
  72. print(response.final_output)
  73. await stdio_server.cleanup()
  74. if __name__ == "__main__":
  75. asyncio.run(run_local_example())
  76. ```
  77. </Tab>
  78. <Tab title="Hosted MCP Server">
  79. This example shows how to connect to a hosted MCP server over HTTP:
  80. ```python
  81. import os
  82. import asyncio
  83. from agency_swarm import Agency, Agent, HostedMCPTool
  84. # Create agent with hosted MCP server
  85. mcp_agent = Agent(
  86. name="HostedMCPAgent",
  87. tools=[
  88. HostedMCPTool(
  89. tool_config={
  90. "type": "mcp",
  91. "server_label": "mcp-tools-server",
  92. # Update with your hosted MCP server URL
  93. "server_url": "https://your-server.com/mcp/",
  94. "require_approval": "never",
  95. "headers": {
  96. "Authorization": (
  97. f"Bearer {os.getenv('APP_TOKEN', 'your-token')}"
  98. )
  99. }
  100. }
  101. ),
  102. ],
  103. )
  104. agency = Agency(mcp_agent)
  105. async def run_hosted_example():
  106. # HostedMCPTools do not require manual connection
  107. response = await agency.get_response("List all mcp tools")
  108. print(response.final_output)
  109. if __name__ == "__main__":
  110. asyncio.run(run_hosted_example())
  111. ```
  112. <Note>
  113. For hosted MCP servers, ensure your server is accessible from the internet.
  114. You can use tools like ngrok for local development.
  115. </Note>
  116. </Tab>
  117. </Tabs>
  118. ## See Also
  119. - [Agents SDK MCP guide](https://openai.github.io/openai-agents-python/mcp/)
  120. - [Agents SDK MCP server reference](https://openai.github.io/openai-agents-python/ref/mcp/server/)