protocol-examples.js•3.84 kB
#!/usr/bin/env node
/**
* MCP Protocol Examples
*
* This file shows the actual JSON-RPC messages that are sent
* between MCP clients and servers. Use this to understand
* the low-level protocol structure.
*/
console.log(`
🔍 MCP Protocol Message Examples
================================
The Model Context Protocol uses JSON-RPC 2.0 messages over stdio.
Here are examples of the messages your server handles:
1️⃣ INITIALIZATION
================
Client sends:
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {
"roots": { "listChanged": true },
"sampling": {}
},
"clientInfo": {
"name": "claude-desktop",
"version": "1.0.0"
}
}
}
Server responds:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {},
"resources": {},
"prompts": {}
},
"serverInfo": {
"name": "learning-mcp-server",
"version": "1.0.0"
}
}
}
2️⃣ LISTING TOOLS
===============
Client sends:
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list"
}
Server responds:
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"tools": [
{
"name": "calculate",
"description": "Perform basic mathematical calculations",
"inputSchema": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate"
}
},
"required": ["expression"]
}
}
]
}
}
3️⃣ CALLING A TOOL
================
Client sends:
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "calculate",
"arguments": {
"expression": "2 + 3 * 4"
}
}
}
Server responds:
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"content": [
{
"type": "text",
"text": "Calculation: 2 + 3 * 4 = 14"
}
]
}
}
4️⃣ LISTING RESOURCES
===================
Client sends:
{
"jsonrpc": "2.0",
"id": 4,
"method": "resources/list"
}
Server responds:
{
"jsonrpc": "2.0",
"id": 4,
"result": {
"resources": [
{
"uri": "file://project-info",
"name": "Project Information",
"description": "Information about this MCP server project",
"mimeType": "text/plain"
}
]
}
}
5️⃣ READING A RESOURCE
====================
Client sends:
{
"jsonrpc": "2.0",
"id": 5,
"method": "resources/read",
"params": {
"uri": "file://project-info"
}
}
Server responds:
{
"jsonrpc": "2.0",
"id": 5,
"result": {
"contents": [
{
"uri": "file://project-info",
"mimeType": "text/plain",
"text": "MCP Learning Server\\n===================="
}
]
}
}
6️⃣ GETTING A PROMPT
==================
Client sends:
{
"jsonrpc": "2.0",
"id": 6,
"method": "prompts/get",
"params": {
"name": "code-review",
"arguments": {
"code": "function add(a, b) { return a + b; }",
"language": "javascript"
}
}
}
Server responds:
{
"jsonrpc": "2.0",
"id": 6,
"result": {
"description": "Code review prompt",
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": "Please review the following javascript code..."
}
}
]
}
}
🎯 Key Takeaways:
================
- All messages follow JSON-RPC 2.0 format
- Each message has: jsonrpc, id, method, params (optional)
- Responses have: jsonrpc, id, result (or error)
- Tools are called with name and arguments
- Resources are identified by URI
- Prompts are templates filled with arguments
📚 To learn more, check out the official MCP documentation!
`);