Repository navigation
Expand file tree
/
Copy pathmcp_server.py
More file actions
658 lines (554 loc) · 19.5 KB
/
Copy pathmcp_server.py
File metadata and controls
658 lines (554 loc) · 19.5 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
"""First-party MCP surface for OpenCLI Admin.
The same server is available over stdio (``opencli-mcp``) and as the built-in
``/mcp`` Streamable HTTP endpoint mounted by :mod:`backend.main`. The official
MCP v2 SDK supplies the 2026-07-28 stateless protocol, ``server/discover``,
cache metadata, and legacy protocol translation.
"""
from __future__ import annotations
import os
from typing import Any
from urllib.parse import urlsplit
import httpx
from mcp.server import MCPServer
from mcp.server.mcpserver import Context
from mcp.server.transport_security import TransportSecuritySettings
from mcp_types import ToolAnnotations
from backend.config import get_settings
MCP_PROTOCOL_VERSION = "2026-07-28"
READ_ONLY_TOOL = ToolAnnotations(
read_only_hint=True,
destructive_hint=False,
idempotent_hint=True,
open_world_hint=True,
)
WRITE_TOOL = ToolAnnotations(
read_only_hint=False,
destructive_hint=False,
idempotent_hint=False,
open_world_hint=True,
)
IDEMPOTENT_WRITE_TOOL = ToolAnnotations(
read_only_hint=False,
destructive_hint=False,
idempotent_hint=True,
open_world_hint=True,
)
def _api_base_url() -> str:
settings = get_settings()
value = getattr(
settings,
"opencli_admin_api_url",
os.environ.get("OPENCLI_ADMIN_API_URL", "http://localhost:8031"),
)
return str(value).rstrip("/")
def _auth_headers(context: Context | None = None) -> dict[str, str]:
"""Separate fleet transport access from the downstream caller identity."""
settings = get_settings()
headers: dict[str, str] = {}
fleet_token = settings.api_auth_token.strip()
if fleet_token:
headers["X-API-Token"] = fleet_token
caller_token = ""
request_headers = context.headers if context is not None else None
if request_headers is not None:
scheme, _, credential = request_headers.get("authorization", "").partition(" ")
if scheme.lower() == "bearer" and credential and credential != fleet_token:
headers["Authorization"] = f"Bearer {credential}"
else:
caller_token = str(
getattr(
settings,
"opencli_mcp_caller_token",
os.environ.get("OPENCLI_MCP_CALLER_TOKEN", ""),
)
).strip()
if caller_token:
headers["Authorization"] = f"Bearer {caller_token}"
return headers
def _context_arg(context: Context | None) -> dict[str, Context]:
"""Keep direct-call compatibility while forwarding real HTTP MCP context."""
return {"context": context} if context is not None else {}
def _csv_env(name: str) -> list[str]:
return [value.strip() for value in os.environ.get(name, "").split(",") if value.strip()]
def _transport_security() -> TransportSecuritySettings:
"""Keep DNS-rebinding protection on while allowing the configured public URL."""
allowed_hosts = [
"127.0.0.1",
"127.0.0.1:*",
"localhost",
"localhost:*",
"[::1]",
"[::1]:*",
"api:*",
]
allowed_origins = [
"http://127.0.0.1",
"http://127.0.0.1:*",
"http://localhost",
"http://localhost:*",
"http://[::1]",
"http://[::1]:*",
]
public_url = get_settings().public_url.strip()
if public_url:
parsed = urlsplit(public_url)
if parsed.netloc:
allowed_hosts.append(parsed.netloc)
allowed_origins.append(f"{parsed.scheme}://{parsed.netloc}")
allowed_hosts.extend(_csv_env("OPENCLI_MCP_ALLOWED_HOSTS"))
allowed_origins.extend(_csv_env("OPENCLI_MCP_ALLOWED_ORIGINS"))
return TransportSecuritySettings(
enable_dns_rebinding_protection=True,
allowed_hosts=list(dict.fromkeys(allowed_hosts)),
allowed_origins=list(dict.fromkeys(allowed_origins)),
)
mcp = MCPServer(
"opencli-admin",
version="0.4.1",
instructions=(
"For a new workflow, first inspect workflow node capabilities, then create a review-only "
"draft from the operator's intent or preview explicit node patches, then compile it. "
"Drafting and compilation never persist or execute work. Run only an immutable published "
"workflow after operator review. Use source tools for collection administration."
),
)
async def _request(
method: str,
path: str,
*,
context: Context | None = None,
**kwargs: Any,
) -> dict[str, Any]:
"""Call the REST API and normalize HTTP/network failures for tool callers."""
try:
api_base_url = _api_base_url()
async with httpx.AsyncClient(
base_url=api_base_url,
timeout=30.0,
headers=_auth_headers(context),
) as client:
response = await client.request(method, path, **kwargs)
try:
body = response.json()
except ValueError:
if response.status_code >= 400:
return {"success": False, "error": response.text}
response.raise_for_status()
raise
if response.status_code >= 400:
error = body.get("detail") or body.get("error") or response.text
return {"success": False, "error": error}
return body
except httpx.HTTPError as exc:
return {
"success": False,
"error": f"request to {_api_base_url()}{path} failed: {exc}",
}
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def list_sources(
enabled: bool | None = None,
channel_type: str | None = None,
page: int = 1,
limit: int = 20,
ctx: Context | None = None,
) -> dict[str, Any]:
"""List configured data sources, optionally filtered by state or channel type."""
params: dict[str, Any] = {"page": page, "limit": limit}
if enabled is not None:
params["enabled"] = enabled
if channel_type is not None:
params["channel_type"] = channel_type
return await _request("GET", "/api/v1/sources", **_context_arg(ctx), params=params)
@mcp.tool(annotations=WRITE_TOOL, structured_output=True)
async def create_source(
name: str,
channel_type: str,
channel_config: dict[str, Any],
description: str | None = None,
enabled: bool = True,
tags: list[str] | None = None,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Create an OpenCLI data source."""
return await _request(
"POST",
"/api/v1/sources",
**_context_arg(ctx),
json={
"name": name,
"channel_type": channel_type,
"channel_config": channel_config,
"description": description,
"enabled": enabled,
"tags": tags or [],
},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def test_source(source_id: str, ctx: Context | None = None) -> dict[str, Any]:
"""Dry-run source connectivity without storing collected records."""
return await _request(
"POST",
f"/api/v1/sources/{source_id}/test",
**_context_arg(ctx),
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def discover_feed(url: str, ctx: Context | None = None) -> dict[str, Any]:
"""Find RSS/Atom feed candidates for a website."""
return await _request(
"POST",
"/api/v1/sources/discover-feed",
**_context_arg(ctx),
json={"url": url},
)
@mcp.tool(annotations=WRITE_TOOL, structured_output=True)
async def trigger_task(
source_id: str,
parameters: dict[str, Any] | None = None,
priority: int = 5,
agent_id: str | None = None,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Dispatch a collection run and return its task identifier."""
return await _request(
"POST",
"/api/v1/tasks/trigger",
**_context_arg(ctx),
json={
"source_id": source_id,
"parameters": parameters or {},
"priority": priority,
"agent_id": agent_id,
},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def get_task(task_id: str, ctx: Context | None = None) -> dict[str, Any]:
"""Read a collection task's durable status."""
return await _request("GET", f"/api/v1/tasks/{task_id}", **_context_arg(ctx))
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def list_records(
source_id: str | None = None,
task_id: str | None = None,
status: str | None = None,
search: str | None = None,
page: int = 1,
limit: int = 20,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Query collected records."""
params: dict[str, Any] = {"page": page, "limit": limit}
if source_id is not None:
params["source_id"] = source_id
if task_id is not None:
params["task_id"] = task_id
if status is not None:
params["status"] = status
if search is not None:
params["search"] = search
return await _request("GET", "/api/v1/records", **_context_arg(ctx), params=params)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def query_project_records(
workspace_id: str,
project_id: str,
q: str | None = None,
limit: int = 20,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Read existing normalized records authorized for one exact project."""
params: dict[str, Any] = {"limit": limit}
if q is not None:
params["q"] = q
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/agent-data/records",
**_context_arg(ctx),
params=params,
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def query_project_context(
workspace_id: str,
project_id: str,
q: str,
limit: int = 8,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Retrieve cited records, knowledge, and durable research without starting work."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/agent-data/context",
**_context_arg(ctx),
params={"q": q, "limit": limit},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def get_project_data_capabilities(
workspace_id: str,
project_id: str,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Read truthful downstream data and research contract availability."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/agent-data/capabilities",
**_context_arg(ctx),
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def get_project_research_readiness(
workspace_id: str,
project_id: str,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Inspect research dependency readiness for an authorized project."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/research/readiness",
**_context_arg(ctx),
)
@mcp.tool(annotations=IDEMPOTENT_WRITE_TOOL, structured_output=True)
async def start_project_research(
workspace_id: str,
project_id: str,
template_id: str,
question: str,
request_id: str,
seed_urls: list[str] | None = None,
max_sources: int = 5,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Start research through the shared REST engine using a stable request ID."""
return await _request(
"POST",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/research/runs",
**_context_arg(ctx),
json={
"template_id": template_id,
"question": question,
"seed_urls": seed_urls or [],
"max_sources": max_sources,
"request_id": request_id,
},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def list_project_research_runs(
workspace_id: str,
project_id: str,
limit: int = 20,
ctx: Context | None = None,
) -> dict[str, Any]:
"""List durable research runs for one authorized project."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/research/runs",
**_context_arg(ctx),
params={"limit": limit},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def get_project_research_run(
workspace_id: str,
project_id: str,
run_id: str,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Read one durable research run after project authorization."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/research/runs/{run_id}",
**_context_arg(ctx),
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def list_project_workflows(
workspace_id: str,
project_id: str,
ctx: Context | None = None,
) -> dict[str, Any]:
"""List a project's workflows and current published versions."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/workflows",
**_context_arg(ctx),
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def list_workflow_node_capabilities(
ctx: Context | None = None,
) -> dict[str, Any]:
"""List typed Workflow nodes, runtime bindings, readiness, and input/output contracts."""
return await _request(
"GET",
"/api/v1/workflows/capabilities",
**_context_arg(ctx),
)
def _new_agent_workflow_project(intent: str, name: str, locale: str) -> dict[str, Any]:
"""Create the canonical review-only seed used by intent-driven drafting."""
return {
"id": "agent-workflow-draft",
"name": name,
"profile": "intelligence",
"version": 1,
"nodes": [
{
"id": "collection-need",
"kind": "schedule",
"capability": "trigger",
"params": {"text": intent, "locale": locale, "mode": "demand-draft"},
"proposalState": "proposed",
"ui": {
"catalogId": "intelligence.input.collection-need",
"label": "Collection Need",
"position": {"x": 160, "y": 180},
},
}
],
"edges": [],
"adapters": [],
"agentPermissions": {
"canFetchNetwork": True,
"canSendNotifications": False,
"canWriteInbox": True,
"canMutateExternalSites": False,
"allowedDomains": [],
},
}
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def draft_workflow_from_intent(
intent: str,
name: str = "Agent workflow draft",
locale: str = "zh-CN",
project: dict[str, Any] | None = None,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Arrange existing nodes into a review-only draft; never persist, publish, or execute it."""
base_project = project or _new_agent_workflow_project(intent, name, locale)
return await _request(
"POST",
"/api/v1/workflows/demand-draft",
**_context_arg(ctx),
json={"project": base_project, "text": intent, "locale": locale},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def preview_workflow_node_patch(
project: dict[str, Any],
operations: list[dict[str, Any]],
ctx: Context | None = None,
) -> dict[str, Any]:
"""Preview explicit add/connect/update node operations without persisting the graph."""
return await _request(
"POST",
"/api/v1/workflows/patch",
**_context_arg(ctx),
json={"project": project, "operations": operations},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def compile_workflow_draft(
project: dict[str, Any],
ctx: Context | None = None,
) -> dict[str, Any]:
"""Validate and compile a draft graph in memory without dispatching or persisting work."""
return await _request(
"POST",
"/api/v1/workflows/compile",
**_context_arg(ctx),
json={"project": project},
)
@mcp.tool(annotations=IDEMPOTENT_WRITE_TOOL, structured_output=True)
async def run_published_workflow(
workspace_id: str,
project_id: str,
workflow_id: str,
inputs: dict[str, Any],
idempotency_key: str,
user: str = "mcp-client",
ctx: Context | None = None,
) -> dict[str, Any]:
"""Run the current immutable published version with an explicit retry key."""
return await _request(
"POST",
(f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs"),
**_context_arg(ctx),
headers={"Idempotency-Key": idempotency_key},
json={"inputs": inputs, "response_mode": "async", "user": user},
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def get_project_runtime_summary(
workspace_id: str,
project_id: str,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Read project-level run counts and recent activity."""
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/runtime-summary",
**_context_arg(ctx),
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def list_project_runtime_logs(
workspace_id: str,
project_id: str,
status: str | None = None,
search: str | None = None,
page: int = 1,
limit: int = 20,
ctx: Context | None = None,
) -> dict[str, Any]:
"""List durable project workflow runs."""
params: dict[str, Any] = {"page": page, "limit": limit}
if status is not None:
params["status"] = status
if search is not None:
params["search"] = search
return await _request(
"GET",
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}/runtime-logs",
**_context_arg(ctx),
params=params,
)
@mcp.tool(annotations=READ_ONLY_TOOL, structured_output=True)
async def get_project_runtime_trace(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
after_sequence: int | None = None,
limit: int | None = None,
ctx: Context | None = None,
) -> dict[str, Any]:
"""Read one project-owned run's projection, checkpoint, and events."""
params: dict[str, Any] = {}
if after_sequence is not None:
params["afterSequence"] = after_sequence
if limit is not None:
params["limit"] = limit
return await _request(
"GET",
(
f"/api/v1/workspaces/{workspace_id}/projects/{project_id}"
f"/workflows/{workflow_id}/runs/{run_id}/trace"
),
**_context_arg(ctx),
params=params,
)
mcp_http_app = mcp.streamable_http_app(
streamable_http_path="/mcp",
json_response=True,
stateless_http=True,
transport_security=_transport_security(),
)
def main() -> None:
transport = os.environ.get("OPENCLI_MCP_TRANSPORT", "stdio")
try:
if transport == "streamable-http":
mcp.run(
transport="streamable-http",
host=os.environ.get("OPENCLI_MCP_HOST", "127.0.0.1"),
port=int(os.environ.get("OPENCLI_MCP_PORT", "8765")),
streamable_http_path="/mcp",
json_response=True,
stateless_http=True,
transport_security=_transport_security(),
)
return
if transport == "stdio":
mcp.run(transport="stdio")
return
raise ValueError("OPENCLI_MCP_TRANSPORT must be 'stdio' or 'streamable-http'")
except KeyboardInterrupt:
raise SystemExit(130) from None
if __name__ == "__main__":
main()