MCP ExplorerExplorer

Mcp Python Sdk

@aniluulinaon 9 months ago
1 MIT
FreeCommunity
AI Systems
mcp in python with troubleshooting

Overview

What is Mcp Python Sdk

mcp-python-sdk is a Python SDK that provides a simple and intuitive interface to interact with MCP, allowing users to create and manage MCP-compatible servers easily.

Use cases

Use cases include developing custom tools for data processing, creating personalized greeting services, and testing server functionalities locally before deployment.

How to use

To use mcp-python-sdk, install the uv package manager, initialize your Python project with uv init, and add MCP to your project using uv add 'mcp[cli]' or pip install mcp. You can then create an MCP server using the provided example code.

Key features

Key features include the ability to define tools and resources, run an MCP server locally, and interact with it using the mcp CLI or Claude Desktop.

Where to use

mcp-python-sdk can be used in various fields such as software development, testing environments, and any application requiring server-client interaction.

Content

Overview

This project provides a simple and intuitive interface to interact with MCP.

It allows you to spin up an MCP-compatible server, define tools and resources, and test locally with mcp CLI or Claude Desktop.


Installation

Install uv

Install the uv package manager recommended by the MCP documentation:

curl -LsSf https://astral.sh/uv/install.sh | sh

Initialize Python project

uv init
# This automatically generates `pyproject.toml` and sets up a virtual environment

Adding MCP to your python project

Documentation recommends installation using uv, but also available with pip

uv add "mcp[cli]"
pip installl mcp

Create simple mcp-server.py

from mcp.server.fastmcp import FastMCP


# Create an MCP server
mcp = FastMCP("mcp_project") 


# Add an additional tool
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b


# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"


if __name__ == "__main__":
    mcp.run()

Virtual environment Notice

When installing or using uv, it may automatically create and activate a virtual environment.

If you’re already inside another virtual environment, make sure to deactivate it to prevent conflicts:

deactivate
source .venv/bin/activate  #it activates the virtual env of your project

Running Your Server via MCP Inspector

mcp dev mcp-server.py

Install mcp-server.py on your Claude Desktop

If the server is running correctly and tools are properly registered, you can install it in Claude Desktop:

mcp install mcp-server.py

Troubleshooting:uvpath issue

If you encounter an error like:

spawn uv ENONET

It means Claude Desktop cannot find your uv binary.

Solution

1. Find the path to your uv installation:

which uv
#it may show the path of uv you use

2.Modify the claude_desktop_config.json to use the full path to uv:

    "mcp_project": {
      "command": "path/to/your/uv", #alter uv into path of uv
      "args": [
        "run",
        "--with",
        "mcp[cli]",
        "mcp",
        "run",
        "/absolute/path/to/your/mcp-server.py"
      ]
    }
  }
}

License

This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers