惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
博客园 - 聂微东
罗磊的独立博客
W
WeLiveSecurity
博客园_首页
Scott Helme
Scott Helme
V
Visual Studio Blog
T
The Exploit Database - CXSecurity.com
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
L
Lohrmann on Cybersecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
F
Full Disclosure
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
雷峰网
雷峰网
博客园 - 【当耐特】
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
T
Tor Project blog
V
V2EX
爱范儿
爱范儿
C
Check Point Blog
T
Threatpost
Project Zero
Project Zero
量子位
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
I
Intezer
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

卡卡罗特

最后代码 | 卡卡罗特 swagger文档 | 卡卡罗特 react脚手架开发 | 卡卡罗特 python的对象增强 | 卡卡罗特 langchain实现Agent | 卡卡罗特 03.python列表集合元组字典 | 卡卡罗特 python依赖打包 | 卡卡罗特 python的异步 | 卡卡罗特 python的生成器 | 卡卡罗特 eino整合Tools | 卡卡罗特 eino实现Agent | 卡卡罗特 eino整合RAG | 卡卡罗特 docker打包go服务 | 卡卡罗特 01-docker入门 | 卡卡罗特 docker-compose | 卡卡罗特 dockerfile | 卡卡罗特 linux安装docker | 卡卡罗特 hertz中间件 | 卡卡罗特 go整合向量数据库ChromaDB | 卡卡罗特 langchainjs | 卡卡罗特 ollama调用各大模型 | 卡卡罗特 spring-ai | 卡卡罗特 context.Context是什么? | 卡卡罗特 01-获取指定网站的所有的链接 | 卡卡罗特 01-Bing每日一图接口 | 卡卡罗特 02-腾讯API高清QQ头像https调用接口 | 卡卡罗特 openCV初体验 | 卡卡罗特 RWMutex读写锁 | 卡卡罗特 SyncMap的使用 | 卡卡罗特 defer的使用 | 卡卡罗特
自定义一个MCP | 卡卡罗特
2026-04-28 · via 卡卡罗特

mcp的要求 ​

必须要实现的方法 ​

一个是mcp的服务描述,一个是头提供那些方法,还有一个调用的能力

go

switch method {
case "initialize":
    sendResponse(id, ...)  // ✅ 必须响应
    
case "tools/list":  
    sendResponse(id, ...)  // ✅ 必须响应
    
case "tools/call":
    sendResponse(id, ...)  // ✅ 有工具时必需
    
default:
    // 对于 notifications/initialized 或其他未知方法
    // 什么都不做即可,不需要报错
    // 因为通知本来就不期待响应
}

标准的请求/响应格式 ​

initialize 请求 ​

json

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "0.1.0",
    "capabilities": {},
    "clientInfo": {
      "name": "Claude Desktop",
      "version": "1.0.0"
    }
  }
}

字段说明

字段类型必填说明
jsonrpcstring固定为 "2.0",表示使用 JSON-RPC 2.0 协议
idnumber/string请求唯一标识,响应时必须原样返回,用于匹配请求和响应
methodstring固定为 "initialize",表示这是一个初始化请求
paramsobject初始化参数对象
params.protocolVersionstring客户端支持的 MCP 协议版本号,目前为 "0.1.0"
params.capabilitiesobject客户端支持的能力列表(如工具、资源、提示等),空对象表示不支持任何扩展能力
params.clientInfoobject客户端信息
params.clientInfo.namestring客户端名称,如 "Claude Desktop""Zed"
params.clientInfo.versionstring客户端版本号

initialize 响应(必须按格式)

json

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "0.1.0",
    "capabilities": {
      "tools": {}
    },
    "serverInfo": {
      "name": "my-server",
      "version": "1.0.0"
    }
  }
}
字段类型必填说明
jsonrpcstring固定为 "2.0"
idnumber/string与请求中的 id 值相同,用于匹配
resultobject响应结果对象(成功时返回)
result.protocolVersionstring服务器选择的协议版本,通常与客户端请求版本一致
result.capabilitiesobject服务器支持的能力列表
result.capabilities.toolsobject如果支持工具功能,此字段存在;空对象 {} 表示支持但暂无特殊配置
result.serverInfoobject服务器信息
result.serverInfo.namestring服务器名称,用于标识这个 MCP 服务
result.serverInfo.versionstring服务器版本号

tools/list 请求 ​

json

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/list"
}

字段说明

字段类型必填说明
jsonrpcstring固定为 "2.0"
idnumber/string请求唯一标识,响应时必须原样返回
methodstring固定为 "tools/list",表示请求获取工具列表

注意tools/list 请求没有 params 字段,因为获取工具列表不需要额外参数。

tools/list 响应(必须按格式)

json

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "tools": [
      {
        "name": "say_hello",
        "description": "打招呼用",
        "inputSchema": {
          "type": "object",
          "properties": {}
        }
      }
    ]
  }
}

字段说明

字段类型必填说明
jsonrpcstring固定为 "2.0"
idnumber/string与请求中的 id 值相同
resultobject响应结果对象
result.toolsarray工具列表数组,可以为空数组 []
result.tools[0]object单个工具对象
result.tools[0].namestring工具名称,用于后续 tools/call 调用时识别
result.tools[0].descriptionstring工具描述,Claude 根据此描述判断何时调用该工具
result.tools[0].inputSchemaobject工具参数的 JSON Schema 定义
inputSchema.typestring固定为 "object",表示参数是一个对象
inputSchema.propertiesobject参数属性定义,每个属性描述一个参数
inputSchema.requiredarray必填参数名称列表

tools/call 请求和响应的完整格式 ​

json

{
  "jsonrpc": "2.0",
  "id": 123,
  "method": "tools/call",
  "params": {
    "name": "to_upper",
    "arguments": {
      "text": "hello world"
    }
  }
}
字段类型必填说明
jsonrpcstring固定为 "2.0"
idnumber/string请求ID,响应时必须原样返回
methodstring固定为 "tools/call"
paramsobject参数对象
params.namestring要调用的工具名称
params.argumentsobject工具参数(根据工具的 inputSchema 定义)

json

{
  "jsonrpc": "2.0",
  "id": 123,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "HELLO WORLD"
      }
    ],
    "isError": false
  }
}
字段类型必填说明
jsonrpcstring固定为 "2.0"
idnumber/string与请求的 id 相同
resultobject结果对象
result.contentarray内容数组,至少一个元素
result.content[].typestring内容类型:"text", "image", "resource"
result.content[].textstring条件type为"text"时必填
result.isErrorboolean是否错误,默认 false

错误响应 ​

json

{
  "jsonrpc": "2.0",
  "id": 123,
  "error": {
    "code": -32601,
    "message": "Tool not found: unknown_tool"
  }
}

自定义一个mcp ​

代码 ​

go

// toupper_mcp.go
package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line := scanner.Text()
		if line == "" {
			continue
		}

		var req map[string]interface{}
		if err := json.Unmarshal([]byte(line), &req); err != nil {
			fmt.Fprintf(os.Stderr, "解析错误: %v\n", err)
			continue
		}

		id, _ := req["id"].(float64)
		method, _ := req["method"].(string)

		switch method {
		case "initialize":
			sendResponse(id, map[string]interface{}{
				"protocolVersion": "2025-03-26",
				"capabilities": map[string]interface{}{
					"tools": map[string]interface{}{}, // 声明支持 tools 能力
				},
				"serverInfo": map[string]string{
					"name":    "toupper-mcp",
					"version": "1.0.0",
				},
			})

		case "tools/list":
			sendResponse(id, map[string]interface{}{
				"tools": []map[string]interface{}{
					{
						"name":        "to_upper",
						"description": "将输入的文本转换为大写",
						"inputSchema": map[string]interface{}{
							"type": "object",
							"properties": map[string]interface{}{
								"text": map[string]string{
									"type":        "string",
									"description": "要转换的文本",
								},
							},
							"required": []string{"text"},
						},
					},
				},
			})

		case "tools/call":
			// 解析工具名称和参数
			params, ok := req["params"].(map[string]interface{})
			if !ok {
				sendError(id, -32602, "Invalid params")
				continue
			}

			toolName, _ := params["name"].(string)
			arguments, _ := params["arguments"].(map[string]interface{})

			if toolName == "to_upper" {
				text, _ := arguments["text"].(string)
				result := strings.ToUpper(text)

				sendResponse(id, map[string]interface{}{
					"content": []map[string]string{
						{
							"type": "text",
							"text": result,
						},
					},
				})
			} else {
				sendError(id, -32601, "Tool not found: "+toolName)
			}

		default:
			// notifications/initialized 或其他方法,忽略即可
			continue
		}
	}
}

func sendResponse(id float64, result interface{}) {
	resp := map[string]interface{}{
		"jsonrpc": "2.0",
		"id":      id,
		"result":  result,
	}
	data, _ := json.Marshal(resp)
	fmt.Println(string(data))
}

func sendError(id float64, code int, message string) {
	resp := map[string]interface{}{
		"jsonrpc": "2.0",
		"id":      id,
		"error": map[string]interface{}{
			"code":    code,
			"message": message,
		},
	}
	data, _ := json.Marshal(resp)
	fmt.Println(string(data))
}

测试 ​

启动服务 输入下面的入参 查看结果

shell

{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}

{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"to_upper","arguments":{"text":"hello"}}}

编译成服务 ​

shell

 go build -o toupper_mcp.exe toupper_mcp.go

MCP Inspector(官方调试工具,最推荐) ​

这是 MCP 官方提供的可视化调试工具,不需要任何 AI 客户端就能测试你的服务

shell

# 一行命令启动
npx @modelcontextprotocol/inspector your-mcp-server

# 如果是 Go 编译的
npx @modelcontextprotocol/inspector ./your-mcp-server

# 如果是 Python 脚本
npx @modelcontextprotocol/inspector python your_mcp_server.py


# 你也可以用命令行参数直接指定 URL,让 Inspector 启动时自动连接
npx @modelcontextprotocol/inspector --url https://your-server.com/mcp

实际

shell

npx @modelcontextprotocol/inspector -- ./toupper_mcp.exe   
 
npx @modelcontextprotocol/inspector -- "E:\go_ws\demo\toupper_mcp.exe"

启动后浏览器会自动打开 http://127.0.0.1:6274,你可以:

点击 List Tools 查看所有工具

点击具体工具,填写参数,Run Tool 直接测试

放到任何一个客户端运行 ​

json

{
  "mcpServers": {
   "toupper": {
      "command": "E:\\go_ws\\demo\\toupper_mcp.exe",
      "args": []
    }
  }
}