MCP开发

1、MCP官方仓库

https://github.com/modelcontextprotocol

2、py开发MCPServer

第一步:mac安装HomeView(国内镜像安装)

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" brew --version

第二步:安装py环境管理UV

brew install uv uv --version

第三步: 初始化python

uv init . -p 3.13

第四步:安装MCP的SDK

uv add "mcp[cli]"

第五步:开发代码

""" FastMCP quickstart example. cd to the examples/snippets/clients directory and run: uv run server fastmcp_quickstart stdio """ from mcp.server.fastmcp import FastMCP # Create an MCP server mcp = FastMCP("Demo") # Add an addition tool @mcp.tool() def add(a: int, b: int) -> int: """Add two numbers""" return a + b if name == "main": mcp.run(transport='sse')

第六步:通过Cherry Studio支持多服务商集成的 AI 对话客户端调试

https://www.cherry-ai.com/

image.png
image.png

第七步:登录硅基流动:https://cloud.siliconflow.cn/获取密钥

image.png

第八步:通过Cherry Studio监测密钥

image.png

第九步:测试效果(多试几次)

image.png

第十步:初始化pypi的package包

uv init . --package -p 3.13 uv add "mcp[cli]"

第十一步:注册pypi(https://pypi.org/

image.png

第十二步:获取token

pypi-AgEIcHlwaS5vcmcCJDJiYzgyMDlhLTI5N2ItNGIwZS1iOTFkLTc3Mjc0MTg0NmVkMAACKlszLCIzZTU3YTE4Ny00OWJiLTRmMmItYjhkNy1jNGU5ZTM3NjcyMDEiXQAABiBR5oNZAv8ENuxExN0FwuxMVGh-fV6Adww8wMjxhfIJVA

第十三步:代码:

""" FastMCP quickstart example. cd to the examples/snippets/clients directory and run: uv run server fastmcp_quickstart stdio """ from mcp.server.fastmcp import FastMCP # Create an MCP server mcp = FastMCP("Demo") # Add an addition tool @mcp.tool() def add(a: int, b: int) -> int: """Add two numbers""" return a + b def main() -> None: mcp.run(transport='stdio')

第十四步:发布:

uv build uv publish --token

第十五步:成功后查看:

image.png

第十六步:MCP服务绑定:

"FNdvu2Cf5yK3nigH_waW6": { "name": "MCP-PYPI-STDIO", "type": "stdio", "description": "", "isActive": true, "registryUrl": "", "command": "uvx", "args": [ "smallants-mcp-pypi" ] }

如果需要发送请求

uv pip install requests  

"""
FastMCP quickstart example.

cd to the `examples/snippets/clients` directory and run:
    uv run server fastmcp_quickstart stdio
"""

from mcp.server.fastmcp import FastMCP

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


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

@mcp.tool()
def get_weather(city: str, unit: str = "celsius") -> str:
    """Get weather for a city."""
    # This would normally call a weather API
    return f"Weather in {city}: 22degrees{unit[0].upper()}"

@mcp.tool()
def get_cloud_user_info(name: str) -> tuple:
    """
    根据用户名调用 API 获取用户信息。
    :param name: 用户名
    :return: 如果查询成功,返回详情数据;否则返回 None
    """
    import requests
    url = ""
    # params = {
    #     "address": address,
    # }
    headers = {
        "token": 
    }
    try:
        response = requests.post(url, headers=headers)
        # 检查响应状态码,如果状态码不是 200,抛出 HTTPError 异常
        # 打印headers
        print(headers)
        response.raise_for_status()
        data = response.json()
        print(data)
        return data
    except requests.RequestException as e:
        print(f"请求出错: {e}")
        return None

if __name__ == "__main__":
    mcp.run(transport='sse')

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。