How to Intermediate · 3 min read

How to connect MCP server to Claude Desktop

Quick answer
To connect an MCP server to Claude Desktop, run an MCP server using the mcp Python SDK with stdio_server transport, then configure Claude Desktop to communicate via standard input/output. This enables seamless interaction between the AI agent and Claude Desktop's local environment.

PREREQUISITES

  • Python 3.8+
  • pip install mcp
  • Claude Desktop installed and running
  • Basic familiarity with command line

Setup MCP server environment

Install the mcp Python package and ensure Claude Desktop is installed on your machine. The MCP server uses standard input/output (stdio) for communication, which Claude Desktop supports natively.

bash
pip install mcp

Step by step MCP server code

Create a Python script to start an MCP server using the stdio_server transport. This server will listen for requests from Claude Desktop and respond accordingly.

python
from mcp.server import Server
from mcp.server.stdio import stdio_server

# Define a simple handler function for MCP requests
def handler(request):
    # Echo back the received request content
    return {"response": f"Received: {request.get('input', '')}"}

# Create the MCP server instance with the handler
server = Server(handler=handler)

# Run the server using stdio transport (compatible with Claude Desktop)
stdio_server(server)

Common variations

  • Use asynchronous MCP server with asyncio for higher throughput.
  • Customize the handler function to integrate with Claude Desktop's specific commands or data formats.
  • Run the MCP server as a background process and connect Claude Desktop via IPC or pipes if stdio is not preferred.
python
import asyncio
from mcp.server import AsyncServer
from mcp.server.stdio import stdio_server

async def async_handler(request):
    # Async processing example
    await asyncio.sleep(0.1)
    return {"response": f"Async received: {request.get('input', '')}"}

async def main():
    server = AsyncServer(handler=async_handler)
    await stdio_server(server)

if __name__ == "__main__":
    asyncio.run(main())

Troubleshooting

  • If Claude Desktop does not receive responses, verify the MCP server is running and connected via stdio.
  • Check for any firewall or permission issues blocking stdio communication.
  • Use logging inside the MCP handler to debug incoming requests and outgoing responses.
  • Ensure Python environment matches the version used to install mcp.

Key Takeaways

  • Use the official mcp Python SDK with stdio_server to connect MCP servers to Claude Desktop.
  • Customize the MCP handler function to process requests from Claude Desktop effectively.
  • Run the MCP server in the same environment as Claude Desktop for seamless stdio communication.
  • Async MCP servers can improve performance for complex integrations.
  • Troubleshoot by verifying stdio connectivity and adding logging inside the MCP server.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗