MCP 协议详细分析一 initialize ping tools/list tools/call
MCP 协议详细分析一 (initialize ping tools/list tools/call)
本节基于 实现一个 java 的mcp client 调用的 一个python 的mcp server 的日志,完整展示一次典型的 MCP Java SDK 通信流程、工具调用、通知机制与日志记录,仅包含 echo-simple 和 echo-with-notifications 两个工具,内容简明、无省略。
2. 详细流程图
3. 详细日志记录(按请求分组)
请求:initialize
=== MCP DEMO CLIENT WITH NOTIFICATIONS AND SAMPLING ===
Connecting to MCP server at: http://localhost:8080
=========================================================
Custom logging HTTP transport created--- Initializing client ---
22:13:36.494 [main] DEBUG i.m.client.McpAsyncClient - Initialization process started
=== ESTABLISHING MCP CONNECTION ===
Target Server URL: http://localhost:8080
SSE Endpoint: http://localhost:8080/sse/
Setting up SSE connection for MCP communication
Handler configured for bidirectional message processing
=====================================
22:13:36.601 [main] DEBUG i.m.spec.McpClientSession - Sending message for method initialize
=== SENDING MCP MESSAGE ===
Target URL: http://localhost:8080/sse/
HTTP Method: POST
Content-Type: application/json
User-Agent: MCP-Java-SDK-Debug/1.0
Authorization: Bearer abcd1234
Message type: JSONRPCRequest
Message content: {"jsonrpc":"2.0","method":"initialize","id":"5ed91d29-0","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"Spring AI MCP Client","version":"0.3.1"}}}
===========================
=== MESSAGE SENT SUCCESSFULLY ===
HTTP POST to http://localhost:8080/sse/ completed
=================================
22:13:36.665 [HttpClient-1-Worker-1] DEBUG i.m.spec.McpSchema - Received JSON message: {"jsonrpc":"2.0","id":"5ed91d29-0","result":{"protocolVersion":"2024-11-05","capabilities":{"experimental":{},"prompts":{"listChanged":true},"resources":{"subscribe":false,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"python mcp server","version":"1.10.1"}}}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCResponse
Message content: {"jsonrpc":"2.0","id":"5ed91d29-0","result":{"protocolVersion":"2024-11-05","capabilities":{"experimental":{},"prompts":{"listChanged":true},"resources":{"subscribe":false,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"python mcp server","version":"1.10.1"}}}
============================
22:13:36.715 [HttpClient-1-Worker-1] DEBUG i.m.spec.McpClientSession - Received Response: JSONRPCResponse[jsonrpc=2.0, id=5ed91d29-0, result={protocolVersion=2024-11-05, capabilities={experimental={}, prompts={listChanged=true}, resources={subscribe=false, listChanged=true}, tools={listChanged=true}}, serverInfo={name=python mcp server, version=1.10.1}}, error=null]
22:13:36.739 [HttpClient-1-Worker-1] INFO i.m.client.McpAsyncClient - Server response with Protocol: 2024-11-05, Capabilities: ServerCapabilities[completions=null, experimental={}, logging=null, prompts=PromptCapabilities[listChanged=true], resources=ResourceCapabilities[subscribe=false, listChanged=true], tools=ToolCapabilities[listChanged=true]], Info: Implementation[name=python mcp server, version=1.10.1] and Instructions null
=== SENDING MCP MESSAGE ===
Target URL: http://localhost:8080/sse/
HTTP Method: POST
Content-Type: application/json
User-Agent: MCP-Java-SDK-Debug/1.0
Authorization: Bearer abcd1234
Message type: JSONRPCNotification
Message content: {"jsonrpc":"2.0","method":"notifications/initialized"}
===========================
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCResponse
=== MESSAGE SENT SUCCESSFULLY ===
HTTP POST to http://localhost:8080/sse/ completed
=================================
Response content: {"jsonrpc":"2.0","id":"5ed91d29-0","result":{"protocolVersion":"2024-11-05","capabilities":{"experimental":{},"prompts":{"listChanged":true},"resources":{"subscribe":false,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"python mcp server","version":"1.10.1"}}}
==============================
Client initialized successfully
请求:ping
--- Pinging server ---
22:13:36.765 [main] DEBUG i.m.client.McpAsyncClient - Joining previous initialization
22:13:36.767 [main] DEBUG i.m.spec.McpClientSession - Sending message for method ping
=== SENDING MCP MESSAGE ===
Target URL: http://localhost:8080/sse/
HTTP Method: POST
Content-Type: application/json
User-Agent: MCP-Java-SDK-Debug/1.0
Authorization: Bearer abcd1234
Message type: JSONRPCRequest
Message content: {"jsonrpc":"2.0","method":"ping","id":"5ed91d29-1"}
===========================
=== MESSAGE SENT SUCCESSFULLY ===
HTTP POST to http://localhost:8080/sse/ completed
=================================
22:13:36.773 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"jsonrpc":"2.0","id":"5ed91d29-1","result":{}}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCResponse
Message content: {"jsonrpc":"2.0","id":"5ed91d29-1","result":{}}
============================
22:13:36.775 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpClientSession - Received Response: JSONRPCResponse[jsonrpc=2.0, id=5ed91d29-1, result={}, error=null]
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCResponse
Ping successful. Response:
{ }
请求:tools/list
--- Listing tools ---
22:13:36.776 [main] DEBUG i.m.client.McpAsyncClient - Joining previous initialization
Response content: {"jsonrpc":"2.0","id":"5ed91d29-1","result":{}}
==============================
22:13:36.777 [main] DEBUG i.m.spec.McpClientSession - Sending message for method tools/list
=== SENDING MCP MESSAGE ===
Target URL: http://localhost:8080/sse/
HTTP Method: POST
Content-Type: application/json
User-Agent: MCP-Java-SDK-Debug/1.0
Authorization: Bearer abcd1234
Message type: JSONRPCRequest
Message content: {"jsonrpc":"2.0","method":"tools/list","id":"5ed91d29-2","params":{}}
===========================
=== MESSAGE SENT SUCCESSFULLY ===
HTTP POST to http://localhost:8080/sse/ completed
=================================
22:13:36.787 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"jsonrpc":"2.0","id":"5ed91d29-2","result":{"tools":[{"name":"echo-simple","description":"简单的Echo工具:直接返回输入的消息(不包装JSON格式)\n\nArgs:\n message: 要回显的消息\n \nReturns:\n 相同的输入消息","inputSchema":{"properties":{"message":{"title":"Message","type":"string"}},"required":["message"],"type":"object"},"outputSchema":{"properties":{"result":{"title":"Result","type":"string"}},"required":["result"],"title":"_WrappedResult","type":"object","x-fastmcp-wrap-result":true}},{"name":"echo-with-notifications","description":"带不同级别通知的Echo工具:演示MCP日志通知功能\n\nArgs:\n message: 要回显的消息\n notification_level: 通知级别\n \nReturns:\n 处理后的消息","inputSchema":{"properties":{"message":{"description":"要回显的消息","title":"Message","type":"string"},"notification_level":{"default":"info","description":"通知级别 (debug/info/warning/error)","title":"Notification Level","type":"string"}},"required":["message"],"type":"object"},"outputSchema":{"properties":{"result":{"title":"Result","type":"string"}},"required":["result"],"title":"_WrappedResult","type":"object","x-fastmcp-wrap-result":true}}]}}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCResponse
Message content: {"jsonrpc":"2.0","id":"5ed91d29-2","result":{"tools":[{"name":"echo-simple","description":"简单的Echo工具:直接返回输入的消息(不包装JSON格式)\n\nArgs:\n message: 要回显的消息\n \nReturns:\n 相同的输入消息","inputSchema":{"properties":{"message":{"title":"Message","type":"string"}},"required":["message"],"type":"object"},"outputSchema":{"properties":{"result":{"title":"Result","type":"string"}},"required":["result"],"title":"_WrappedResult","type":"object","x-fastmcp-wrap-result":true}},{"name":"echo-with-notifications","description":"带不同级别通知的Echo工具:演示MCP日志通知功能\n\nArgs:\n message: 要回显的消息\n notification_level: 通知级别\n \nReturns:\n 处理后的消息","inputSchema":{"properties":{"message":{"description":"要回显的消息","title":"Message","type":"string"},"notification_level":{"default":"info","description":"通知级别 (debug/info/warning/error)","title":"Notification Level","type":"string"}},"required":["message"],"type":"object"},"outputSchema":{"properties":{"result":{"title":"Result","type":"string"}},"required":["result"],"title":"_WrappedResult","type":"object","x-fastmcp-wrap-result":true}}]}}
============================
22:13:36.791 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpClientSession - Received Response: JSONRPCResponse[jsonrpc=2.0, id=5ed91d29-2, result={tools=[{name=echo-simple, description=简单的Echo工具:直接返回输入的消息(不包装JSON格式)Args:message: 要回显的消息Returns:相同的输入消息, inputSchema={properties={message={title=Message, type=string}}, required=[message], type=object}, outputSchema={properties={result={title=Result, type=string}}, required=[result], title=_WrappedResult, type=object, x-fastmcp-wrap-result=true}}, {name=echo-with-notifications, description=带不同级别通知的Echo工具:演示MCP日志通知功能Args:message: 要回显的消息notification_level: 通知级别Returns:处理后的消息, inputSchema={properties={message={description=要回显的消息, title=Message, type=string}, notification_level={default=info, description=通知级别 (debug/info/warning/error), title=Notification Level, type=string}}, required=[message], type=object}, outputSchema={properties={result={title=Result, type=string}}, required=[result], title=_WrappedResult, type=object, x-fastmcp-wrap-result=true}}]}, error=null]
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCResponse--- Testing Notifications Feature ---
Response content: {"jsonrpc":"2.0","id":"5ed91d29-2","result":{"tools":[{"name":"echo-simple","description":"简单的Echo工具:直接返回输入的消息(不包装JSON格式)\n\nArgs:\n message: 要回显的消息\n \nReturns:\n 相同的输入消息","inputSchema":{"properties":{"message":{"title":"Message","type":"string"}},"required":["message"],"type":"object"},"outputSchema":{"properties":{"result":{"title":"Result","type":"string"}},"required":["result"],"title":"_WrappedResult","type":"object","x-fastmcp-wrap-result":true}},{"name":"echo-with-notifications","description":"带不同级别通知的Echo工具:演示MCP日志通知功能\n\nArgs:\n message: 要回显的消息\n notification_level: 通知级别\n \nReturns:\n 处理后的消息","inputSchema":{"properties":{"message":{"description":"要回显的消息","title":"Message","type":"string"},"notification_level":{"default":"info","description":"通知级别 (debug/info/warning/error)","title":"Notification Level","type":"string"}},"required":["message"],"type":"object"},"outputSchema":{"properties":{"result":{"title":"Result","type":"string"}},"required":["result"],"title":"_WrappedResult","type":"object","x-fastmcp-wrap-result":true}}]}}
==============================
请求:tools/call(echo-with-notifications)
Request params:
{"name" : "echo-with-notifications","arguments" : {"notification_level" : "warning","message" : "测试通知功能!"}
}
22:13:36.807 [main] DEBUG i.m.client.McpAsyncClient - Joining previous initialization
22:13:36.808 [main] DEBUG i.m.spec.McpClientSession - Sending message for method tools/call
=== SENDING MCP MESSAGE ===
Target URL: http://localhost:8080/sse/
HTTP Method: POST
Content-Type: application/json
User-Agent: MCP-Java-SDK-Debug/1.0
Authorization: Bearer abcd1234
Message type: JSONRPCRequest
Message content: {"jsonrpc":"2.0","method":"tools/call","id":"5ed91d29-3","params":{"name":"echo-with-notifications","arguments":{"notification_level":"warning","message":"测试通知功能!"}}}
===========================
=== MESSAGE SENT SUCCESSFULLY ===
HTTP POST to http://localhost:8080/sse/ completed
=================================
22:13:36.820 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"method":"notifications/message","params":{"level":"debug","data":"这是一个调试级别的通知"},"jsonrpc":"2.0"}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCNotification
Message content: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"debug","data":"这是一个调试级别的通知"}}
============================
22:13:36.823 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpClientSession - Received notification: JSONRPCNotification[jsonrpc=2.0, method=notifications/message, params={level=debug, data=这是一个调试级别的通知}]
22:13:36.842 [HttpClient-1-Worker-2] DEBUG i.m.client.McpAsyncClient - Logging: LoggingMessageNotification[level=DEBUG, logger=null, data=这是一个调试级别的通知]=== NOTIFICATION RECEIVED ===
Level: DEBUG
Logger: null
Data: 这是一个调试级别的通知
=============================
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCNotification
Response content: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"debug","data":"这是一个调试级别的通知"}}
==============================
22:13:36.845 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"method":"notifications/message","params":{"level":"info","data":"这是一个信息级别的通知"},"jsonrpc":"2.0"}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCNotification
Message content: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","data":"这是一个信息级别的通知"}}
============================
22:13:36.847 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpClientSession - Received notification: JSONRPCNotification[jsonrpc=2.0, method=notifications/message, params={level=info, data=这是一个信息级别的通知}]
22:13:36.848 [HttpClient-1-Worker-2] DEBUG i.m.client.McpAsyncClient - Logging: LoggingMessageNotification[level=INFO, logger=null, data=这是一个信息级别的通知]=== NOTIFICATION RECEIVED ===
Level: INFO
Logger: null
Data: 这是一个信息级别的通知
=============================
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCNotification
Response content: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","data":"这是一个信息级别的通知"}}
==============================
22:13:36.850 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"method":"notifications/message","params":{"level":"warning","data":"警告:您选择了警告级别来处理消息: 测试通知功能!"},"jsonrpc":"2.0"}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCNotification
Message content: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"warning","data":"警告:您选择了警告级别来处理消息: 测试通知功能!"}}
============================
22:13:36.851 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpClientSession - Received notification: JSONRPCNotification[jsonrpc=2.0, method=notifications/message, params={level=warning, data=警告:您选择了警告级别来处理消息: 测试通知功能!}]
22:13:36.852 [HttpClient-1-Worker-2] DEBUG i.m.client.McpAsyncClient - Logging: LoggingMessageNotification[level=WARNING, logger=null, data=警告:您选择了警告级别来处理消息: 测试通知功能!]=== NOTIFICATION RECEIVED ===
Level: WARNING
Logger: null
Data: 警告:您选择了警告级别来处理消息: 测试通知功能!
=============================
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCNotification
Response content: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"warning","data":"警告:您选择了警告级别来处理消息: 测试通知功能!"}}
==============================
22:13:36.854 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"jsonrpc":"2.0","id":"5ed91d29-3","result":{"content":[{"type":"text","text":"{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"}],"structuredContent":{"result":"{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"},"isError":false}}
=== INCOMING MCP MESSAGE ===
Received from: http://localhost:8080/sse/
Message type: JSONRPCResponse
Message content: {"jsonrpc":"2.0","id":"5ed91d29-3","result":{"content":[{"type":"text","text":"{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"}],"structuredContent":{"result":"{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"},"isError":false}}
============================
22:13:36.856 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpClientSession - Received Response: JSONRPCResponse[jsonrpc=2.0, id=5ed91d29-3, result={content=[{type=text, text={"result": true, "message": "带warning级别通知的Echo成功", "timestamp": "2025-07-19T22:13:36.819802Z", "tool_name": "echo_with_notifications", "data": {"echo_message": "测试通知功能!", "notification_level": "warning", "notifications_sent": ["debug", "info", "warning"]}}}], structuredContent={result={"result": true, "message": "带warning级别通知的Echo成功", "timestamp": "2025-07-19T22:13:36.819802Z", "tool_name": "echo_with_notifications", "data": {"echo_message": "测试通知功能!", "notification_level": "warning", "notifications_sent": ["debug", "info", "warning"]}}}, isError=false}, error=null]
=== OUTGOING MCP RESPONSE ===
Sending to: http://localhost:8080/sse/
Response type: JSONRPCResponse
Response content: {"jsonrpc":"2.0","id":"5ed91d29-3","result":{"content":[{"type":"text","text":"{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"}],"structuredContent":{"result":"{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"},"isError":false}}
==============================
Notification tool response:
{"content" : [ {"type" : "text","text" : "{\"result\": true, \"message\": \"带warning级别通知的Echo成功\", \"timestamp\": \"2025-07-19T22:13:36.819802Z\", \"tool_name\": \"echo_with_notifications\", \"data\": {\"echo_message\": \"测试通知功能!\", \"notification_level\": \"warning\", \"notifications_sent\": [\"debug\", \"info\", \"warning\"]}}"} ],"isError" : false
}--- Waiting for notifications ---
Timeout waiting for notifications, received: 3 out of 5 expected
请求:closeGracefully
--- Closing client ---
=== CLOSING MCP CONNECTION ===
Closing connection to: http://localhost:8080/sse/
Initiating graceful shutdown of transport
Transport closed successfully
==============================
Client closed successfully
4. 工具列表与参数(本次仅包含 echo-simple、echo-with-notifications)
22:13:36.787 [HttpClient-1-Worker-2] DEBUG i.m.spec.McpSchema - Received JSON message: {"jsonrpc":"2.0","id":"5ed91d29-2","result":{"tools":[{"name": "echo-simple","description": "简单的Echo工具:直接返回输入的消息(不包装JSON格式)\n\nArgs:\n message: 要回显的消息\n \nReturns:\n 相同的输入消息","inputSchema": {"properties": {"message": { "title": "Message", "type": "string" }},"required": ["message"],"type": "object"},"outputSchema": {"properties": {"result": { "title": "Result", "type": "string" }},"required": ["result"],"title": "_WrappedResult","type": "object","x-fastmcp-wrap-result": true}},{"name": "echo-with-notifications","description": "带不同级别通知的Echo工具:演示MCP日志通知功能\n\nArgs:\n message: 要回显的消息\n notification_level: 通知级别\n \nReturns:\n 处理后的消息","inputSchema": {"properties": {"message": { "description": "要回显的消息", "title": "Message", "type": "string" },"notification_level": { "default": "info", "description": "通知级别 (debug/info/warning/error)", "title": "Notification Level", "type": "string" }},"required": ["message"],"type": "object"},"outputSchema": {"properties": {"result": { "title": "Result", "type": "string" }},"required": ["result"],"title": "_WrappedResult","type": "object","x-fastmcp-wrap-result": true}}
]}}