claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

Help with setting MCP servers?

Open amitok2 opened this issue 1 year ago • 5 comments

Hey guys, can someone help me with setting mcp servers? what files should I create and how to configure it to the ClaudeCodeOptions?

amitok2 avatar Jul 23 '25 09:07 amitok2

something like that

      servers = {
           # custom MCP server in my app module
           "e2b-mcp-server": {
               "command": "python",
               "args": ["-m", "app.mcp.e2b_mcp_server"],
               "env": {
                   "PYTHONPATH": str(Path(__file__).parent.parent.parent),
                   "E2B_API_KEY": e2b_api_key,
               },
            },
           "github": {
               "command": "npx",
               "args": ["-y", "@modelcontextprotocol/server-github"],
               "env": {
                   "GITHUB_PERSONAL_ACCESS_TOKEN": github_personal_access_token
               },
            },
        }
 
     options = ClaudeCodeOptions(
          mcp_servers=servers,
      )

Mng-dev-ai avatar Jul 23 '25 18:07 Mng-dev-ai

I must to run this mcp server in order to use it? Can I simply create a tool (simple function to use) to pass to the Clade code? i dont want overcomplex it with McpServer ..

amitok2 avatar Jul 30 '25 13:07 amitok2

no you don't need to run the mcp server and I don't think claude code supports tools right now

Mng-dev-ai avatar Jul 30 '25 19:07 Mng-dev-ai

Okay, so can u please explain to me little but more, lets say i want to use MCP for custom bash commands. I need to create the python mcp server for that (with the function), and how should i pass to Claude code this file? And if i dont run the mcp server how claude code communicates with it?

amitok2 avatar Jul 30 '25 19:07 amitok2

MCP server configuration is now well-documented. You can configure MCP servers in two ways:

  1. External MCP servers via mcp_servers in ClaudeAgentOptions:
options = ClaudeAgentOptions(
    mcp_servers={
        "my_server": {
            "type": "stdio",
            "command": "npx",
            "args": ["-y", "@your/mcp-server"]
        }
    }
)
  1. In-process SDK MCP servers via create_sdk_mcp_server() and the @tool decorator:
from claude_agent_sdk import tool, create_sdk_mcp_server

@tool("greet", "Greet a user", {"name": str})
async def greet(args):
    return {"content": [{"type": "text", "text": f"Hello {args['name']}!"}]}

server = create_sdk_mcp_server("my_tools", tools=[greet])
options = ClaudeAgentOptions(mcp_servers={"my_tools": server})

See the SDK documentation and the examples/ directory in this repo for more details. Closing as answered.

qing-ant avatar Mar 24 '26 12:03 qing-ant