MCP从入门到实战

MCP介绍

大多数资料都会说是一种模型上下文协议,对!它仅仅就是一个协议,协议只是一种规范,让它变得通用,所以一些支持MCP的AI大模型的客户端也只不过是遵循了这种协议,让她得以调用MCP服务器中的工具

什么是MCP服务器呢

MCP服务器是一个符合 MCP 协议的、可被大语言模型(LLM)通过标准化方式调用的"服务提供者"或"工具接口",也就是当你向MCP服务器发送某个数据包的时候,MCP服务器会返回符合MCP协议的数据包,传输协议一般是标准输入输出stdio,或者远程的https,下面会使用Node为例搭建一个简单的MCP服务器(任何其它语言也可以,只需满足MCP的通信协议)

MCP的用处

简单来说就是为大模型赋能,因为只要你的MCP服务器可以做的功能,大模型这边就可以调用你的MCP服务器中的工具实现任何你想要的功能,比如一般来说大模型是无法操作你的电脑对文件进行操作的,但是只要在你的MCP服务器中实现一个工具:对文件进行读写等,那么你的大模型就可以调用这个工具实现对本地电脑文件的操作了,是不是很强呢

MCP基本通信格式

1. 初始化

request

json 复制代码
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "roots": {
        "listChanged": true
      },
      "sampling": {},
      "elicitation": {}
    },
    "clientInfo": {
      "name": "ExampleClient",
      "title": "Example Client Display Name",
      "version": "1.0.0"
    }
  }
}

response

json 复制代码
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "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"
    },
    "instructions": "Optional instructions for the client"
  }
}

2. 工具发现

request

json 复制代码
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}

response

json 复制代码
{
  "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 or zip code"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}

3. 工具调用

request

json 复制代码
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "get_weather",
    "arguments": {
      "location": "New York"
    }
  }
}

response

json 复制代码
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Current weather in New York:\nTemperature: 72°F\nConditions: Partly cloudy"
      }
    ],
    "isError": false
  }
}

Node实现一个基础的MCP服务器

javascript 复制代码
const readline = require('readline');

// 创建readline接口
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

// ============== 工具定义 ==============
const tools = [
  {
    name: "sum",
    title: "求和",
    description: "计算两数之和",
    inputSchema: {
      type: "object",
      properties: {
        a: { 
          type: "number", 
          description: "第一个数" 
        },
        b: { 
          type: "number", 
          description: "第二个数" 
        }
      },
      required: ["a", "b"]
    }
  }
];

// ============== 工具处理器 ==============
const toolHandlers = {
  // 计算两数之和
  sum: ({ a, b }) => {
    if (typeof a !== 'number' || typeof b !== 'number') {
      throw new Error("Both arguments must be numbers");
    }
    return `${a} + ${b} = ${a + b}`;
  }
};

// ============== 请求处理器 ==============
const requestHandlers = {
  // 处理初始化请求
  "initialize": (params, id) => ({
    jsonrpc: "2.0",
    id,
    result: {
      protocolVersion: params.protocolVersion,
      capabilities: {
        prompts: { listChanged: true },
        resources: { subscribe: true, listChanged: true },
        tools: { listChanged: true }
      },
      serverInfo: {
        name: "XiaoZheMCP",
        title: "XiaoZhe MCP Server",
        version: "0.0.0"
      },
      instructions: "测试"
    }
  }),
  
  // 返回工具列表
  "tools/list": (_, id) => ({
    jsonrpc: "2.0",
    id,
    result: { tools }
  }),
  
  // 处理工具调用
  "tools/call": async (params, id) => {
    const { name, arguments: args } = params;
    const handler = toolHandlers[name];
    
    try {
      // 检查工具是否存在
      if (!handler) {
        throw new Error(`Tool '${name}' not found`);
      }
      
      // 执行工具处理
      const result = await handler(args);
      
      return {
        jsonrpc: "2.0",
        id,
        result: {
          content: [{ type: "text", text: result }],
          isError: false
        }
      };
    } catch (error) {
      return {
        jsonrpc: "2.0",
        id,
        error: {
          code: -32000,
          message: "Tool execution failed",
          data: error.message
        }
      };
    }
  }
};

// ============== 主处理逻辑 ==============
rl.on('line', async (line) => {
  try {
    const request = JSON.parse(line);
    const handler = requestHandlers[request.method];
    
    if (!handler) {
      throw new Error(`Unsupported method: ${request.method}`);
    }
    
    // 处理请求并发送响应
    const response = await handler(request.params, request.id);
    console.log(JSON.stringify(response));
    
  } catch (error) {
    // 处理解析错误
    const errorResponse = {
      jsonrpc: "2.0",
      id: null,
      error: {
        code: -32700,
        message: "Parse error",
        data: error.message
      }
    };
    console.error(JSON.stringify(errorResponse));
  }
});

MCP调试工具

sh 复制代码
npx @modelcontextprotocol/inspector

执行后完成后会自动打开一个网页,然后就可以愉快的测试了

官方库快速开发MCP服务器

安装依赖包:

sh 复制代码
npm i @modelcontextprotocol/sdk
sh 复制代码
npm i zod

示例代码:

javascript 复制代码
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from 'zod';

const server = new McpServer({
  name: "XiaoZheMCP",
  title: "XiaoZhe MCP Server",
  version: "1.0.0",
});

// 创建工具
server.registerTool(
    'sum',
    {
        title: '求和',
        description: '计算两数之和',
        inputSchema: {
            a: z.number().describe('第一个数'),
            b: z.number().describe('第二个数')
        }
    },
    ({a, b})=>{
        return {
            content: [
                {
                    type: 'text',
                    text: `${a} + ${b} = ${a + b}`
                }
            ]
        }
    }
)

const transport = new StdioServerTransport();
server.connect(transport);

最后

可以使用任意支持MCP服务器和大模型的客户端,这样就可以使用对话的方式和自己的MCP服务器交互了!!

相关推荐
猫头虎1 分钟前
Claude Code 2026 年1月9日迎来大更新:Agent 能力增强(2.1.0 详解 + 升级指南)
ide·人工智能·macos·langchain·编辑器·aigc·ai编程
飞哥数智坊20 分钟前
3位实战分享、6个案例展示,TRAE Friends@济南第二场圆满完成
人工智能·ai编程·trae
向量引擎1 小时前
复刻“疯狂的鸽子”?用Python调用Sora2与Gemini-3-Pro实现全自动热点视频流水线(附源码解析)
开发语言·人工智能·python·gpt·ai·ai编程·api调用
winfredzhang1 小时前
从零构建:基于 Node.js 的全栈视频资料管理系统开发实录
css·node.js·html·音视频·js·收藏,搜索,缩略图
遗憾随她而去.1 小时前
Webpack 面试题
前端·webpack·node.js
恋猫de小郭1 小时前
Tailwind 因为 AI 的裁员“闹剧”结束,而 AI 对开源项目的影响才刚刚开始
前端·flutter·ai编程
清沫10 小时前
Claude Skills:Agent 能力扩展的新范式
前端·ai编程
程序员佳佳11 小时前
【万字硬核】从零构建企业级AI中台:基于Vector Engine整合GPT-5.2、Sora2与Veo3的落地实践指南
人工智能·gpt·chatgpt·ai作画·aigc·api·ai编程
小小小小小鹿12 小时前
# 险些酿成P0事故!我用 AI 打造了 Android 代码评审“守门员”
agent·ai编程
野生的码农12 小时前
做好自己的份内工作,等着被裁
程序员·ai编程·vibecoding