SDK Bug: Fix TypeError With Create_sdk_mcp_server()

by Alex Johnson 52 views

Agent deployment failing with a TypeError? It's likely due to the create_sdk_mcp_server() function in the claude-agent-sdk-python. This article dives into the root cause, providing a clear explanation and a minimal reproduction example so you can understand and address the issue.

Understanding the Problem

The issue arises because create_sdk_mcp_server() attempts to pass a version parameter to the Server.__init__() method of the mcp package. However, the Server.__init__() method only accepts the name parameter. This incompatibility leads to a TypeError. Let's break it down further.

The Technical Details

Specifically, the SDK code in claude_agent_sdk/__init__.py looks like this:

def create_sdk_mcp_server(
    name: str, version: str = "1.0.0", tools: list[SdkMcpTool[Any]] | None = None
) -> McpSdkServerConfig:
    from mcp.server import Server
    # ...
    server = Server(name, version=version)  # ❌ version not supported!

Notice that the version parameter is being passed to the Server constructor.

On the other hand, the MCP Server Class in all versions from 0.9.1 to 1.21.0 defines its initializer as follows:

class Server:
    def __init__(self, name: str):  # Only accepts name!
        self.name = name
        # ...

As you can see, the Server.__init__ method only accepts the name parameter. The version parameter is simply not supported, hence the TypeError.

Environment Details

  • SDK Version: claude-agent-sdk 0.1.6 (Python)
  • MCP Version: mcp 0.9.1 (also tested with 1.21.0 - same issue)
  • Python Version: 3.11.13
  • OS: macOS (Darwin 24.6.0)

This issue is consistent across different versions of the mcp package, indicating that the problem lies within how the SDK is calling the Server class.

Reproducing the Error: A Minimal Example

To demonstrate the issue, you can use the following code snippet:

from claude_agent_sdk import tool, create_sdk_mcp_server

@tool("test_tool", "A test tool", {"input": str})
async def test_tool(args: dict) -> dict:
    return {"content": [{"type": "text", "text": "Test"}]}

# This line fails with TypeError
server = create_sdk_mcp_server(
    name="test_server",
    version="1.0.0",  # ← SDK passes this to Server() but it's not accepted
    tools=[test_tool]
)

When you run this code, you'll encounter the following error:

Traceback (most recent call last):
  File "src/claude_agent_sdk/__init__.py", line 147, in create_sdk_mcp_server
    server = Server(name, version=version)
TypeError: Server.__init__() got an unexpected keyword argument 'version'

This confirms that the version parameter is indeed causing the problem.

Root Cause Analysis

The root cause of this issue is a mismatch between the parameters that create_sdk_mcp_server() passes to the Server class and the parameters that the Server.__init__() method actually accepts. The SDK is attempting to pass a version parameter, but the Server class is not designed to handle it.

This discrepancy likely stems from an oversight in the SDK's development, where the create_sdk_mcp_server() function was incorrectly implemented to include the version parameter. Examination of the MCP Server class reveals that the version parameter has never been supported. Checking the signatures of the Server.__init__ method in different versions of mcp confirms this:

  • mcp==0.9.1: Server.__init__(self, name: str)
  • mcp==1.21.0: Server.__init__(self, name: str) (unchanged)

The signatures clearly show that the Server.__init__ method only accepts the name parameter.

Potential Solutions and Workarounds

While a proper fix would involve updating the SDK to remove the unsupported version parameter, here are some immediate workarounds you could consider:

  1. Modify the SDK Code (Temporary):
    • You could directly modify the create_sdk_mcp_server() function in your local copy of the SDK to remove the version=version argument when calling Server(). This is a quick fix but will be overwritten when you update the SDK.
  2. Create a Wrapper Function:
    • You could create your own wrapper function around create_sdk_mcp_server() that omits the version parameter. This would isolate the change and prevent it from affecting other parts of your code.

Example Wrapper Function:

from claude_agent_sdk import create_sdk_mcp_server, tool

@tool("test_tool", "A test tool", {"input": str})
async def test_tool(args: dict) -> dict:
    return {"content": [{"type": "text", "text": "Test"}]}

def create_sdk_mcp_server_no_version(
    name: str, tools: list[SdkMcpTool[Any]] | None = None
):
    return create_sdk_mcp_server(name=name, version="1.0.0", tools=tools)

# Use the wrapper function
server = create_sdk_mcp_server_no_version(
    name="test_server", tools=[test_tool]
)

print(server)

This wrapper function effectively calls the original create_sdk_mcp_server() but removes the problematic version parameter. However, this is not a clean solution, and it's more of a walkaround than the final solution.

The Importance of Reporting Bugs

This bug highlights the importance of reporting issues when you encounter them. By reporting this issue, you contribute to improving the quality and reliability of the SDK for everyone.

Conclusion

The TypeError caused by create_sdk_mcp_server() passing an unsupported version parameter is a significant issue that prevents agent deployment. By understanding the root cause and using the provided workarounds, you can mitigate the problem until a proper fix is released. Remember to keep your SDK and MCP versions in mind and stay informed about updates that address this issue.

For more information on the Anthropic Claude API and related SDKs, visit the official Anthropic website at Anthropic. This will provide you with updates, documentation, and community resources for further exploration.