MCP-学习(2)

105 阅读3分钟

MCP

MCP是一套标准协议,它规定了应用程序之间如何通信

如何通信:

  • 通信方式
    • stdio:推荐(高效、简洁、本地)
    • http:可远程
  • 通信格式:基于JSON-RPC的进一步规范

官方网址:modelcontextprotocol.io

基本规范

1. 初始化 initialize

request

{
    "jsonrpc": "2.0",  //JSON-RPC的版本
    "id": 1,  //响应时 id要一一对应
    "method": "initialize",  //固定为initialize
    "params": {
        "protocolVersion": "2025-06-18",  //MCP的版本,官网查看
        "capabilities": {
            "roots": {
                "listChanged": true
            },
            "sampling": {},
            "elicitation": {}
        },
        "clientInfo": {  //告知服务器 客户端的信息
            "name": "",
            "title": "",
            "version": "1.0.0"
        }
    }
}

response

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "protocolVersion": "2025-06-18",
        "capabilities": {  // 服务器具有哪些功能
            "logging": {},
            "prompts": {
                "listChanged": true
            },
            "resources": {
                "subscribe": true,
                "listChanged": true
            },
            "tools": {
                "listChanged": true
            }
        },
        "serverInfo": {  //服务端信息
            "name": "ExampleServer",
            "title": "Example Server Display Name",
            "version": "1.0.0"
        },
        "instrutions": "Optional instructions for the client"
    }
}

2. 工具发现 tools/list

服务器有哪些工具函数可以供客户端调用

request

{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/list",  //表示:列出服务器的工具
    "params": {}
}

response

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "tools": [
            {
                "name": "get_weather",
                "title": "Weather Information Provider",
                "description": "Get current weather information for a location",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "City name"
                        }
                    },
                    "required": ["location"]
                }
            }
        ]
    }
}

3. 工具调用 tools/call

request

{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/call",  //调用工具
    "params": {
        "name": "get_weather", //工具名,对应工具发现中的name
        "arguments": {  // 工具参数,需要和工具发现中的结构一致
            "location": "New York"
        }
    }
}

response

{
    "jsonrpc": "2.0",
    "id": 2,
    "result": {
        "content": [{  // 函数结果需要放到content字段中,使用数组
            "type": "text",  // 函数结果类型
            "text": "72℉"
        }]
    }
}

支持的类型: modelcontextprotocol.io/specificati… (找到Data Types)

工具和效率

以下为Node环境的一些工具,聊的都是node环境的开发:

MCP Server 的调试工具

如果需要快速地测试我们地MCP服务器地功能,有以下方式可以便捷测试: 直接运行

npx @modelcontextprotocol/inspector

运行完之后会打开一个本地的网页,作为MCP的客户端。在里面设置一下配置,Connect连接服务器(也就是启动服务器)。

History中就可以看到客户端和服务器的所有通信。(它会过滤掉一些繁杂的信息,没看到是正常的)

MCP SDK (便捷开发MCP服务器)

使用@modelcontextprotocol/sdk可以更方便的开发MCP Server

npm install @modelcontextprotocol/sdk

切记!,在服务器的函数中如果想要调试的话,不能使用console.log(123)去做打印,因为这样会污染stdout标准输出,可以使用console.error(123)这种形式来调试,这个叫做标准错误输出接口

对接AI应用程序

什么是AI应用程序

所有能与大模型交互的应用都可以看作AI应用程序

常见的AI应用程序:

  • ChatGPT
  • Deepseek Chat Page
  • Cursor

尝试在Cursor中使用本地的MCP能力

只要有了MCP服务器,打开Cursor的MCP配置,为了防止Cursor找不到我们的功能,最好使用绝对路径:

{
    "mcpServers": {
        "MCP_test_Server": {  // MCP服务器配置
            "command": "通过where node查找即可",  // 通常填"node"即可,为防止Cursor找不到node路径,最好上传绝对路径,
            "args": ["MCP服务器的绝对路径"]  // 参数(可能是多个,所以是数组)
        }
    }
}

之后就可以在Cursor中看到自己的MCP能力,它在合适的时候就会调用对应的能力来搞定对应的事情。

image.png

两个核心概念:

  • MCP Host:往往指代AI应用本身,用于发现MCP Server以及其中的工具列表
  • MCP Client:用于和MCP Server通信的客户端,往往在Host内部开启,通常情况下,每启动一个MCP Server,就会开启一个MCP Client。 image.png

重新认识MCP

MCP,全称Model Context Protocal,模型上下文协议。其旨在为AI与外部程序之间建立通信标准,从而使得外部程序可以被部署到任意AI,也使得AI应用可以使用任意的外部程序

MCP资源聚合平台

  1. github.com/modelcontex…
  2. mcpservers.org
  3. mcp.so
  4. modelscope.cn/mcp