-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcp_stdio_server.py
More file actions
executable file
·206 lines (187 loc) · 6.53 KB
/
mcp_stdio_server.py
File metadata and controls
executable file
·206 lines (187 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
"""
Simple MCP (Model Context Protocol) stdio server example.
This is a minimal MCP server that communicates over stdin/stdout using JSON-RPC 2.0.
It implements a basic calculator with add, subtract, multiply, and divide operations.
"""
import sys
import json
def handle_tools_list(request_id):
"""Return the list of available tools."""
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {
"tools": [
{
"name": "add",
"description": "Add two numbers",
"inputs": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"required": ["a", "b"]
},
"outputs": {"type": "number"},
"tags": ["math", "calculator"]
},
{
"name": "subtract",
"description": "Subtract two numbers",
"inputs": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"required": ["a", "b"]
},
"outputs": {"type": "number"},
"tags": ["math", "calculator"]
},
{
"name": "multiply",
"description": "Multiply two numbers",
"inputs": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"required": ["a", "b"]
},
"outputs": {"type": "number"},
"tags": ["math", "calculator"]
},
{
"name": "divide",
"description": "Divide two numbers",
"inputs": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "Numerator"},
"b": {"type": "number", "description": "Denominator"}
},
"required": ["a", "b"]
},
"outputs": {"type": "number"},
"tags": ["math", "calculator"]
}
]
}
}
def handle_tools_call(request_id, params):
"""Execute a tool call."""
tool_name = params.get("name")
arguments = params.get("arguments", {})
try:
a = arguments.get("a")
b = arguments.get("b")
if a is None or b is None:
return {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32602,
"message": "Missing required arguments 'a' and 'b'"
}
}
# Perform the calculation
if tool_name == "add":
result = a + b
elif tool_name == "subtract":
result = a - b
elif tool_name == "multiply":
result = a * b
elif tool_name == "divide":
if b == 0:
return {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32000,
"message": "Division by zero"
}
}
result = a / b
else:
return {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32601,
"message": f"Unknown tool: {tool_name}"
}
}
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {
"result": result,
"tool": tool_name,
"arguments": arguments
},
"final": True
}
except Exception as e:
return {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32000,
"message": str(e)
}
}
def main():
"""Main event loop for the MCP server."""
# Log to stderr so it doesn't interfere with JSON-RPC on stdout
print("MCP Calculator Server started", file=sys.stderr)
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
request = json.loads(line)
method = request.get("method")
request_id = request.get("id", 1)
params = request.get("params", {})
# Handle different MCP methods
if method == "tools/list":
response = handle_tools_list(request_id)
elif method == "tools/call":
response = handle_tools_call(request_id, params)
else:
response = {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32601,
"message": f"Method not found: {method}"
}
}
# Send response to stdout
print(json.dumps(response), flush=True)
except json.JSONDecodeError as e:
error_response = {
"jsonrpc": "2.0",
"id": None,
"error": {
"code": -32700,
"message": f"Parse error: {e}"
}
}
print(json.dumps(error_response), flush=True)
except Exception as e:
error_response = {
"jsonrpc": "2.0",
"id": None,
"error": {
"code": -32603,
"message": f"Internal error: {e}"
}
}
print(json.dumps(error_response), flush=True)
if __name__ == "__main__":
main()