Repository navigation
Expand file tree
/
Copy pathmcp.py
More file actions
291 lines (272 loc) · 10.3 KB
/
Copy pathmcp.py
File metadata and controls
291 lines (272 loc) · 10.3 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Scoped MCP stdio bridge; credentials stay in process environment, never tool args."""
from __future__ import annotations
import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
from . import __version__
PROTOCOLS = ("2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05")
def tool(name, description, properties, required=()):
return dict(
name=name,
description=description,
inputSchema=dict(
type="object",
properties=properties,
required=list(required),
additionalProperties=False,
),
)
S = {"type": "string"}
TOOLS = [
tool(
"agent_list", "List teammates in this run's project. Does not start them.", {}
),
tool(
"message_send",
"Dispatch bounded work to a teammate. In a project task only the owner can dispatch, into task-scoped conversations. Choose worker or reviewer. Finish this turn; results return automatically. Use idempotency_key for retries.",
{
"recipient_id": S,
"recipient_session_id": S,
"body": S,
"correlation_id": S,
"idempotency_key": S,
"task_role": {"type": "string", "enum": ["worker", "reviewer"]},
},
("recipient_id", "body"),
),
tool(
"task_status",
"Inspect the execution state and result of a task you sent or received. This does not wait for completion.",
{"message_id": S},
("message_id",),
),
tool(
"memory_search",
"Search approved, non-archived project memory using literal keywords.",
{"query": S},
),
tool(
"memory_propose",
"Propose a memory update for human review. Cannot overwrite approved memory. expected_version is zero for a new key.",
{"key": S, "content": S, "expected_version": {"type": "integer", "minimum": 0}},
("key", "content", "expected_version"),
),
tool(
"task_context",
"Read this project task: goal, exact acceptance criteria, saved inputs/decisions, workspaces and previous results. No access to other tasks.",
{},
),
tool(
"task_history",
"Read durable task records in order, including full requirements, inputs, decisions and prior reports. Start after=0,offset=0; Read up to 20 records per page; continue with next_after and next_offset until records is empty. The final record may contain partial JSON text.",
{
"after": {"type": "integer", "minimum": 0},
"offset": {"type": "integer", "minimum": 0},
"limit": {"type": "integer", "minimum": 1, "maximum": 20},
},
),
tool(
"task_result",
"Read a full raw result in this task in pages. Check next_offset; never rely only on truncated handoff text.",
{"run_id": S, "offset": {"type": "integer", "minimum": 0}},
("run_id",),
),
tool(
"task_ask",
"Save a question that needs a human decision. Finish your turn after asking. Answers persist and resume the task owner automatically.",
{"question": S, "options": {"type": "array", "items": S, "maxItems": 8}},
("question",),
),
tool(
"task_deliver",
"Task owner submits a delivery. Cover every exact acceptance criterion once, with honest validation evidence. Finishing a native turn alone does not accept the task.",
{
"summary": S,
"checks": {
"type": "array",
"minItems": 1,
"maxItems": 30,
"items": {
"type": "object",
"properties": {
"criterion": S,
"status": {
"type": "string",
"enum": ["passed", "failed", "unverified"],
},
"evidence": S,
},
"required": ["criterion", "status", "evidence"],
"additionalProperties": False,
},
},
"artifacts": {"type": "array", "items": S, "maxItems": 30},
"risks": S,
},
("summary", "checks"),
),
tool(
"task_review",
"Independent reviewer records its verdict on the owner workspace and current requirements. Report findings honestly; unverified or changes_requested blocks required review acceptance.",
{
"verdict": {
"type": "string",
"enum": ["approved", "changes_requested", "unverified"],
},
"summary": S,
},
("verdict", "summary"),
),
]
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
class Bridge:
def __init__(self, call):
self.call = call
self.initialized = False
def handle(self, message):
if not isinstance(message, dict):
return {
"jsonrpc": "2.0",
"id": None,
"error": {"code": -32600, "message": "Invalid request"},
}
identifier = message.get("id")
method = message.get("method")
if message.get("jsonrpc") != "2.0" or not isinstance(method, str):
return {
"jsonrpc": "2.0",
"id": identifier,
"error": {"code": -32600, "message": "Invalid request"},
}
if "id" not in message:
return None
try:
params = message.get("params", {})
if not isinstance(params, dict):
raise ValueError("Invalid parameters")
if method == "initialize":
requested = params.get("protocolVersion")
self.initialized = True
result = {
"protocolVersion": requested
if requested in PROTOCOLS
else PROTOCOLS[0],
"capabilities": {"tools": {"listChanged": False}},
"serverInfo": {"name": "agentdock", "version": __version__},
}
elif not self.initialized:
raise ValueError("Initialize first")
elif method == "ping":
result = {}
elif method == "tools/list":
result = {"tools": TOOLS}
elif method == "tools/call":
name = params.get("name")
args = params.get("arguments", {})
definition = next((t for t in TOOLS if t["name"] == name), None)
if definition is None:
raise ValueError("Unknown tool")
schema = definition["inputSchema"]
if (
not isinstance(args, dict)
or set(args) - set(schema["properties"])
or any(x not in args for x in schema["required"])
):
raise ValueError("Invalid tool arguments")
try:
value = self.call(name, args)
result = {
"content": [
{
"type": "text",
"text": json.dumps(value, ensure_ascii=False),
}
],
"isError": False,
}
except Exception:
result = {
"content": [
{
"type": "text",
"text": "Tool request rejected or unavailable. Check the workbench; no action should be assumed successful.",
}
],
"isError": True,
}
else:
return {
"jsonrpc": "2.0",
"id": identifier,
"error": {"code": -32601, "message": "Method not found"},
}
return {"jsonrpc": "2.0", "id": identifier, "result": result}
except (ValueError, TypeError):
return {
"jsonrpc": "2.0",
"id": identifier,
"error": {"code": -32602, "message": "Invalid parameters or lifecycle"},
}
def main():
address = os.environ.get("AGENTDOCK_URL", "")
parsed = urllib.parse.urlsplit(address)
if (
parsed.scheme != "http"
or parsed.hostname != "127.0.0.1"
or not parsed.port
or parsed.path not in ("", "/")
or parsed.query
or parsed.fragment
or parsed.username
):
print("AgentDock MCP requires an explicit loopback address", file=sys.stderr)
return 2
token = os.environ.get("AGENTDOCK_CAPABILITY", "")
if not token:
print("AgentDock MCP requires a run capability", file=sys.stderr)
return 2
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
def invoke(name, args):
req = urllib.request.Request(
address.rstrip("/") + "/mcp/tool",
data=json.dumps({"name": name, "arguments": args}).encode(),
headers={
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
method="POST",
)
with opener.open(req, timeout=15) as response:
data = response.read(1048577)
if len(data) > 1048576:
raise ValueError("Response too large")
return json.loads(data)
bridge = Bridge(invoke)
while True:
raw = sys.stdin.buffer.readline(262145)
if not raw:
break
if len(raw) > 262144:
print("MCP input exceeds limit", file=sys.stderr)
return 2
try:
message = json.loads(raw)
except (ValueError, UnicodeError):
result = {
"jsonrpc": "2.0",
"id": None,
"error": {"code": -32700, "message": "Parse error"},
}
else:
result = bridge.handle(message)
if result is not None:
sys.stdout.write(json.dumps(result, ensure_ascii=False) + "\n")
sys.stdout.flush()
return 0
if __name__ == "__main__":
raise SystemExit(main())