Stdio Transport
Overview
Section titled “Overview”The Stdio (Standard Input/Output) transport is designed for connecting MCPServerAdapter to local MCP servers that communicate over their standard input and output streams. This is typically used when the MCP server is a script or executable running on the same machine as your CrewAI application.
Key Concepts
Section titled “Key Concepts”- Local Execution: Stdio transport manages a locally running process for the MCP server.
StdioServerParameters: This class from themcplibrary is used to configure the command, arguments, and environment variables for launching the Stdio server.
Connecting via Stdio
Section titled “Connecting via Stdio”You can connect to an Stdio-based MCP server using two main approaches for managing the connection lifecycle:
1. Fully Managed Connection (Recommended)
Section titled “1. Fully Managed Connection (Recommended)”Using a Python context manager (with statement) is the recommended approach. It automatically handles starting the MCP server process and stopping it when the context is exited.
from crewai import Agent, Task, Crew, Processfrom crewai_tools import MCPServerAdapterfrom mcp import StdioServerParametersimport os
# Create a StdioServerParameters objectserver_params=StdioServerParameters( command="python3", args=["servers/your_stdio_server.py"], env={"UV_PYTHON": "3.12", **os.environ},)
with MCPServerAdapter(server_params) as tools: print(f"Available tools from Stdio MCP server: {[tool.name for tool in tools]}")
# Example: Using the tools from the Stdio MCP server in a CrewAI Agent research_agent = Agent( role="Local Data Processor", goal="Process data using a local Stdio-based tool.", backstory="An AI that leverages local scripts via MCP for specialized tasks.", tools=tools, reasoning=True, verbose=True, )
processing_task = Task( description="Process the input data file 'data.txt' and summarize its contents.", expected_output="A summary of the processed data.", agent=research_agent, markdown=True )
data_crew = Crew( agents=[research_agent], tasks=[processing_task], verbose=True, process=Process.sequential )
result = data_crew.kickoff() print("\nCrew Task Result (Stdio - Managed):\n", result)2. Manual Connection Lifecycle
Section titled “2. Manual Connection Lifecycle”If you need finer-grained control over when the Stdio MCP server process is started and stopped, you can manage the MCPServerAdapter lifecycle manually.
from crewai import Agent, Task, Crew, Processfrom crewai_tools import MCPServerAdapterfrom mcp import StdioServerParametersimport os
# Create a StdioServerParameters objectstdio_params=StdioServerParameters( command="python3", args=["servers/your_stdio_server.py"], env={"UV_PYTHON": "3.12", **os.environ},)
mcp_server_adapter = MCPServerAdapter(server_params=stdio_params)try: mcp_server_adapter.start() # Manually start the connection and server process tools = mcp_server_adapter.tools print(f"Available tools (manual Stdio): {[tool.name for tool in tools]}")
# Example: Using the tools with your Agent, Task, Crew setup manual_agent = Agent( role="Local Task Executor", goal="Execute a specific local task using a manually managed Stdio tool.", backstory="An AI proficient in controlling local processes via MCP.", tools=tools, verbose=True )
manual_task = Task( description="Execute the 'perform_analysis' command via the Stdio tool.", expected_output="Results of the analysis.", agent=manual_agent )
manual_crew = Crew( agents=[manual_agent], tasks=[manual_task], verbose=True, process=Process.sequential )
result = manual_crew.kickoff() # Actual inputs depend on your tool print("\nCrew Task Result (Stdio - Manual):\n", result)
except Exception as e: print(f"An error occurred during manual Stdio MCP integration: {e}")finally: if mcp_server_adapter and mcp_server_adapter.is_connected: # Check if connected before stopping print("Stopping Stdio MCP server connection (manual)...") mcp_server_adapter.stop() # **Crucial: Ensure stop is called** elif mcp_server_adapter: # If adapter exists but not connected (e.g. start failed) print("Stdio MCP server adapter was not connected. No stop needed or start failed.")Remember to replace placeholder paths and commands with your actual Stdio server details. The env parameter in StdioServerParameters can
be used to set environment variables for the server process, which can be useful for configuring its behavior or providing necessary paths (like PYTHONPATH).