import sys
import json
def send(msg):
sys.stdout.write(json.dumps(msg) + "\n")
sys.stdout.flush()
def handle(method, params, req_id):
if method == "initialize":
return send({
"jsonrpc": "2.0", "id": req_id,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {"tools": {}},
"serverInfo": {"name": "test-mcp", "version": "1.0"}
}
})
elif method == "tools/list":
return send({
"jsonrpc": "2.0", "id": req_id,
"result": {
"tools": [{
"name": "echo",
"description": "Echoes back the message argument",
"inputSchema": {
"type": "object",
"properties": {
"message": {"type": "string", "description": "The message to echo"}
},
"required": ["message"]
}
}]
}
})
elif method == "tools/call":
tool_name = params.get("name", "")
tool_args = params.get("arguments", {})
if tool_name == "echo":
msg = tool_args.get("message", "")
return send({
"jsonrpc": "2.0", "id": req_id,
"result": {
"content": [{"type": "text", "text": f"ECHO: {msg}"}],
"isError": False
}
})
else:
return send({
"jsonrpc": "2.0", "id": req_id,
"error": {"code": -32601, "message": f"Tool not found: {tool_name}"}
})
else:
return send({
"jsonrpc": "2.0", "id": req_id,
"error": {"code": -32601, "message": f"Method not found: {method}"}
})
def main():
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
msg = json.loads(line)
except json.JSONDecodeError:
continue
method = msg.get("method", "")
params = msg.get("params", {})
req_id = msg.get("id")
if req_id is None:
continue handle(method, params, req_id)
if __name__ == "__main__":
main()