使用python给cursor增加一个mcp
1、安装python库
https://pypi.org/project/fastmcp/
2、fastmcp官网
https://gofastmcp.com/getting-started/welcome
3、参考视频 https://www.bilibili.com/video/BV1rE1SBpEha/?spm_id_from=333.1391.0.0&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea
手撸一个简单的mpc
服务端
# -*- coding: utf-8 -*-
from fastmcp import FastMCP
from starlette.requests import Request
from starlette.responses import PlainTextResponse
# 初始化 MCP 服务实例
mcp = FastMCP(name="MyServer")
# 定义一个工具函数
@mcp.tool
def greet(name: str) -> str:
"""Greet a user by name. 这里是工具函数的描述 大模型会根据这个描述来调用这个工具函数"""
return f"Hello, {name}!"
# 资源
@mcp.resource("data://config")
def get_config () -> dict:
"""Get the weather of a city. 这里是资源函数的描述 大模型会根据这个描述来调用这个资源函数"""
return {"city": "city", "weather": "sunny"}
# 参数
@mcp.prompt
def analyze_data(data: str) -> str:
"""分析数据的 prompt。这里是 prompt 的描述,大模型会根据这个描述来调用这个 prompt"""
return f"我是prompt{data}"
# 定义一个自定义路由(健康检查)
@mcp.custom_route("/health/{name}", methods=["GET"])
async def health_check(request: Request) -> PlainTextResponse:
"""健康检查端点"""
name = request.path_params.get("name", "World")
return PlainTextResponse(f"Hello, {name}!")
if __name__ == "__main__":
# 启动服务(Streamable HTTP 模式)
mcp.run(transport="streamable-http", host="127.0.0.1", port=9000)
客户端
# -*- coding: utf-8 -*-
# 工具调用示例
from fastmcp import Client
import asyncio
async def main():
async with Client("http://localhost:9000/mcp") as client:
tools = await client.list_tools() # 获取工具列表
# print(tools)
result = await client.call_tool("greet", arguments={"name": "World"}) # 调用工具
# print(result)
resources = await client.list_resources() # 获取资源列表
# print(resources)
config = await client.read_resource("data://config") # 获取资源
# print(config)
prompt = await client.list_prompts() # 获取参数列表
print("Prompts列表:", prompt)
result = await client.get_prompt("analyze_data",{"data": "World"}) # 调用参数
print("Prompt结果:", result)
if __name__ == "__main__":
asyncio.run(main())
配置到cursor
在cursor的设置里面 配置即可

例子
{
"mcpServers": {
"12306-mcp": {
"type": "streamable_http",
"url": "https://mcp.api-inference.modelscope.net/1839194735ed4e/mcp"
}
}
}
可以在魔搭社区 or github 找mcp使用

浙公网安备 33010602011771号