第一种:使用cursor来使用MCP
首先需要下载最新版本的cursor:
下载地址
下载安装登录后就可以在设置处看到MCP Servers

点击 +Add

这里就是添加MCP Server的位置,下面是一些提供的网址:
- Awesome MCP Servers:提供丰富多样的 MCP 服务器列表,涵盖多种类别,支持中文,方便开发者快速找到合适的服务器资源。
- Smithery.ai:提供即拿即用的 MCP 服务器配置,支持多平台使用,简化集成过程。
- MCP.so:一站式 MCP 资源平台,同时提供完整的 MCP Server 和实用的 MCP Client。
- Model Context Protocol 官方网站:提供官方权威的 MCP 示例和信息,强调安全可靠的集成方式,是学习和开发 MCP 应用的基础资源。
- MCP Server / Client空间站:中文网址
在这里使用高德的MCP Server进行示范:网址

右边的服务器配置就是要配置进cursor的json文件中,申请高德api_key后填入

保存后显示绿灯就是ok了

接下来进行使用(一定要选择agent)

可以看到调用了MCP tools,也就是刚才添加的。点击Run tool即可
如果要添加多个Servers只需要去网址找到对应的npx命令,然后添加即可,例如:(注意放置位置)

第二种,使用VS Code来使用MCP
下载拓展Cline,其实就是vs code版本的Cursor
打开后填入api_key则可使用

点击设置后,也是跟第一个办法一样写入json文件即可。如果是Windows的话就需要修改一下:

第三种,结合LangChain或者Langgraph使用
安装对应的库pip install langchain-mcp-adapters
运行逻辑是先定义一个MCP服务,再从langchain里面使用
示例代码如下:
MCP Server:
# math_server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Math")
@mcp.tool()
def add(a: int, b: int) -> int:
"""将两个数字相加"""
return a + b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""将两个数字相乘"""
return a * b
if __name__ == "__main__":
mcp.run(transport="stdio")
接着记得要启动才能待会被调用
LangChain Client:
# 创建异步函数
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
import asyncio
from langchain.schema.messages import AIMessage
# 初始化 LLM 模型,我这里用的是https://bigmodel.cn/的模型,可以自己替换
model = ChatOpenAI(
model_name="glm-4-flash",
api_key="xxxx",
base_url="https://open.bigmodel.cn/api/paas/v4/",
)
# 设置 MCP 服务器参数
server_params = StdioServerParameters(
command="python",
args=[r"D:\Project\agri_puppy\mcp-server-demo\math_server.py"], # 确保这是 math_server.py 的完整绝对路径
)
# 创建异步函数
async def async_main():
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await load_mcp_tools(session)
agent = create_react_agent(model, tools)
agent_response = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"})
print("Final answer:", agent_response["messages"][-1].content)
# 调用异步函数
asyncio.run(async_main())
'''
Final answer: The result of (3 + 5) x 12 is 96.
'''

274

被折叠的 条评论
为什么被折叠?



