mcp-integration.mdx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ---
  2. title: 'MCP Integration'
  3. description: 'Connect your agents to external tools and data using the Model Context Protocol (MCP).'
  4. icon: "plug"
  5. ---
  6. Agency Swarm agents can interact with a wider range of tools and data sources beyond their built-in capabilities by using the **Model Context Protocol (MCP)**. MCP is an open standard ([view specification](https://github.com/modelcontextprotocol/modelcontextprotocol)) that allows agents to communicate with external services like local file systems, databases, or custom APIs, as long as those services implement the protocol.
  7. Think of MCP as a universal translator that lets your agent talk to specialized external tools.
  8. ## Why use MCP?
  9. * **Access Local Resources:** Let agents read/write local files or run local commands.
  10. * **Connect to Custom Services:** Integrate with proprietary APIs or internal tools without writing specific Agency Swarm tool wrappers for each one, provided an MCP server exists.
  11. * **Leverage Existing MCP Tools:** Utilize third-party tools that already support MCP.
  12. ## Supported MCP Server Types
  13. Agency Swarm provides helpers to connect to MCP servers using the Agents SDK. Choose the server type based on how your tool provider operates:
  14. <Accordion title="MCPServerStdio: For Command-Line Tools" icon="terminal">
  15. Use this if your tool server is a **command-line program or script**. Agency Swarm will start this program for you and communicate with it directly using its standard input/output.
  16. * **When to use:** Your tool is a local script, an executable, or requires running a specific command to be activated.
  17. ```python
  18. from agents.mcp.server import MCPServerStdio, MCPServerStdioParams
  19. stdio_server = MCPServerStdio(
  20. MCPServerStdioParams(
  21. command="python", # or 'npx' if available
  22. args=["./path/to/your/mcp_server.py"]
  23. ),
  24. cache_tools_list=True
  25. )
  26. ```
  27. </Accordion>
  28. <Accordion title="MCPServerSse: For Web Service Tools" icon="globe-pointer">
  29. Use this if your tool server is already **running as a web service** at a specific **HTTP URL**. Agency Swarm connects to this URL to access tools exposed via Server-Sent Events (SSE).
  30. * **When to use:** Your tool is provided by a web API, a microservice, or any server accessible via an HTTP endpoint that speaks MCP+SSE.
  31. ```python
  32. from agents.mcp.server import MCPServerSse, MCPServerSseParams
  33. sse_server = MCPServerSse(
  34. MCPServerSseParams(
  35. url="http://localhost:8000/sse/",
  36. headers={
  37. "Authorization": "Bearer secret-token"
  38. }
  39. )
  40. )
  41. ```
  42. </Accordion>
  43. <Accordion title="MCPServerStreamableHttp: For HTTP Streaming Tools" icon="globe-pointer">
  44. Use this if your tool server is a **web service** that implements the **Streamable HTTP** transport protocol. This uses HTTP POST requests with optional Server-Sent Events (SSE) streaming for responses.
  45. * **When to use:** Your tool server operates as an independent web service, supports multiple client connections, needs stateful or stateless operation, or requires server-to-client streaming capabilities.
  46. ```python
  47. from agents.mcp.server import MCPServerStreamableHttp, MCPServerStreamableHttpParams
  48. streamable_http_server = MCPServerStreamableHttp(
  49. MCPServerStreamableHttpParams(
  50. url="http://localhost:8000/mcp/",
  51. headers={
  52. "Authorization": "Bearer secret-token"
  53. }
  54. )
  55. )
  56. ```
  57. </Accordion>
  58. <Accordion title="HostedMCPTool: For Public Web Servers" icon="globe">
  59. Use this if your tool server is a **publicly accessible web service**. This approach uses OpenAI's hosted MCP tool capabilities.
  60. * **When to use:** Your tool server is accessible from the internet and you want to leverage OpenAI's infrastructure for MCP connections.
  61. ```python
  62. from agency_swarm import HostedMCPTool
  63. hosted_tool = HostedMCPTool(
  64. tool_config={
  65. "type": "mcp",
  66. "server_label": "mcp-tools-server",
  67. # For http servers:
  68. "server_url": "https://your-server.com/mcp/",
  69. # For sse servers
  70. # "server_url": "https://your-server.com/sse/",
  71. "require_approval": "never",
  72. "headers": {
  73. "Authorization": f"Bearer secret-token"
  74. }
  75. }
  76. )
  77. ```
  78. <Note>
  79. Server specified in the HostedMCPTool should be publicly accessible for the agent to be able to use this tool
  80. </Note>
  81. </Accordion>
  82. ## Connecting Agents to MCP Servers
  83. To give an agent access to MCP tools, you define the server connections and pass them to the agent's `mcp_servers` list or `tools` list during initialization. Follow these steps:
  84. <Accordion title="Step 1: Define Local Server Connections">
  85. Configure local MCP servers (stdio, local SSE, or streamable HTTP) that will run on your machine.
  86. ```python
  87. from agents.mcp.server import MCPServerStdio, MCPServerStdioParams
  88. # Example: Stdio server for local tools
  89. stdio_server = MCPServerStdio(
  90. MCPServerStdioParams(
  91. command="python",
  92. args=["./examples/utils/stdio_mcp_server.py"]
  93. ),
  94. cache_tools_list=True
  95. )
  96. ```
  97. </Accordion>
  98. <Accordion title="Step 2: Define Hosted Server Tools (Optional)">
  99. Configure hosted MCP tools for publicly accessible servers.
  100. ```python
  101. from agency_swarm import HostedMCPTool
  102. hosted_tool = HostedMCPTool(
  103. tool_config={
  104. "type": "mcp",
  105. "server_label": "mcp-tools-server",
  106. "server_url": "https://your-public-server.com/mcp/",
  107. "require_approval": "never",
  108. "headers": {
  109. "Authorization": f"Bearer {os.getenv('APP_TOKEN', 'your-token')}"
  110. }
  111. }
  112. )
  113. ```
  114. </Accordion>
  115. <Accordion title="Step 3: Initialize Agent with Servers">
  116. Pass the configured server connections to the appropriate parameter when creating your `Agent`.
  117. ```python
  118. from agency_swarm import Agent
  119. # For local MCP servers, use mcp_servers parameter
  120. local_agent = Agent(
  121. name="LocalMCPAgent",
  122. description="An agent that can use local MCP tools.",
  123. instructions="Use the available MCP tools to help users.",
  124. mcp_servers=[stdio_server], # Local servers go here
  125. model="gpt-5.4-mini",
  126. )
  127. # For hosted MCP servers, use tools parameter
  128. hosted_agent = Agent(
  129. name="HostedMCPAgent",
  130. description="An agent that can use hosted MCP tools.",
  131. instructions="Use the available hosted MCP tools to help users.",
  132. tools=[hosted_tool], # Hosted tools go here
  133. model="gpt-5.4-mini",
  134. )
  135. ```
  136. </Accordion>
  137. <Note>
  138. You can use `shared_mcp_servers` parameter inside agency to attach an MCP server to all agents at once.
  139. </Note>
  140. ## Runnable Demo
  141. For a practical, runnable example using both local and hosted MCP servers, see the complete example above or the `mcp_server_example.py` script located in the `examples/` directory of the Agency Swarm repository.
  142. * **Remember:** The demo requires you exposing port 8000 through ngrok prior to running it.
  143. ## Key Takeaways
  144. * MCP connects agents to external tools/data via standard protocols (Stdio, SSE, Streamable HTTP).
  145. * Use mcp_server for local mcp servers and `HostedMCPTool` for web-based servers, since the latter allows you to avoid maintaining server lifecycle.
  146. * External MCP servers must be running separately for the agent to connect to them.
  147. ## See Also
  148. - [Agents SDK MCP guide](https://openai.github.io/openai-agents-python/mcp/)