-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconduit_mcp_server.py
More file actions
474 lines (451 loc) · 17 KB
/
conduit_mcp_server.py
File metadata and controls
474 lines (451 loc) · 17 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
"""Standalone stdio MCP server for Conduit.
Run with: python conduit_mcp_server.py
or via: python -m conduit_mcp_server
"""
from __future__ import annotations
import asyncio
import json
import sys
from typing import Any
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import TextContent, Tool
# ---------------------------------------------------------------------------
# Lazy bridge — only import heavy deps when first tool call arrives
# ---------------------------------------------------------------------------
_bridge: Any = None
async def _get_bridge():
global _bridge
if _bridge is None:
try:
from tools.conduit_bridge import ConduitBridge
except ImportError:
# Fallback path when installed as package
import importlib
cb = importlib.import_module("conduit_bridge", package="tools")
ConduitBridge = cb.ConduitBridge
_bridge = ConduitBridge()
await _bridge.start()
return _bridge
# ---------------------------------------------------------------------------
# Tool definitions
# ---------------------------------------------------------------------------
TOOLS = [
Tool(
name="conduit_navigate",
description="Navigate to a URL in the stealth headless browser.",
inputSchema={
"type": "object",
"properties": {
"url": {"type": "string", "description": "URL to navigate to"},
},
"required": ["url"],
},
),
Tool(
name="conduit_screenshot",
description="Take a screenshot of the current browser page.",
inputSchema={
"type": "object",
"properties": {
"full_page": {"type": "boolean", "default": False},
},
},
),
Tool(
name="conduit_extract",
description="Extract text content from the current page.",
inputSchema={
"type": "object",
"properties": {
"selector": {"type": "string", "description": "CSS selector (optional, defaults to body)"},
},
},
),
Tool(
name="conduit_click",
description="Click an element on the current page.",
inputSchema={
"type": "object",
"properties": {
"selector": {"type": "string", "description": "CSS selector of element to click"},
},
"required": ["selector"],
},
),
Tool(
name="conduit_fill",
description="Fill a form field with text.",
inputSchema={
"type": "object",
"properties": {
"selector": {"type": "string"},
"text": {"type": "string"},
},
"required": ["selector", "text"],
},
),
Tool(
name="conduit_search",
description="Search the web using multiple engines (DuckDuckGo, Brave, etc.).",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"},
"engine": {"type": "string", "default": "ddg", "description": "Engine: ddg, brave, exa, tavily"},
"num_results": {"type": "integer", "default": 10},
},
"required": ["query"],
},
),
Tool(
name="conduit_eval",
description="Execute JavaScript on the current page. Source is stored verbatim in audit chain.",
inputSchema={
"type": "object",
"properties": {
"script": {"type": "string", "description": "JavaScript to execute"},
},
"required": ["script"],
},
),
Tool(
name="conduit_export_proof",
description="Export a self-verifiable cryptographic proof bundle of all actions taken.",
inputSchema={
"type": "object",
"properties": {
"output_path": {"type": "string", "description": "Path to write the .tar.gz proof bundle"},
},
},
),
Tool(
name="conduit_marketplace_list",
description="List marketplace product metadata and supported marketplaces.",
inputSchema={
"type": "object",
"properties": {},
},
),
Tool(
name="conduit_marketplace_create_job",
description="Create a queued marketplace extraction job for Upwork or Fiverr.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string", "description": "Marketplace slug: upwork or fiverr"},
"target_type": {"type": "string", "description": "Marketplace target type"},
"target_url": {"type": "string", "description": "Target page URL"},
"account_id": {"type": "string", "description": "Optional marketplace account id"},
"proxy_label": {"type": "string", "description": "Optional proxy label"},
"request_payload": {"type": "object", "description": "Optional request metadata"},
},
"required": ["marketplace", "target_type", "target_url"],
},
),
Tool(
name="conduit_marketplace_targets",
description="List supported target types for a marketplace.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string", "description": "Marketplace slug: upwork or fiverr"},
},
"required": ["marketplace"],
},
),
Tool(
name="conduit_marketplace_plan",
description="Build a normalized marketplace extraction plan before creating a job.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string", "description": "Marketplace slug: upwork or fiverr"},
"target_type": {"type": "string", "description": "Marketplace target type"},
"target_url": {"type": "string", "description": "Target page URL"},
"account_id": {"type": "string", "description": "Optional marketplace account id"},
"proxy_label": {"type": "string", "description": "Optional proxy label"},
},
"required": ["marketplace", "target_type", "target_url"],
},
),
Tool(
name="conduit_marketplace_create_account",
description="Create a named marketplace account record for session reuse.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string"},
"display_name": {"type": "string"},
"credential_key": {"type": "string"},
"proxy_label": {"type": "string"},
"metadata": {"type": "object"},
},
"required": ["marketplace", "display_name"],
},
),
Tool(
name="conduit_marketplace_create_proxy",
description="Create or update a named marketplace proxy route.",
inputSchema={
"type": "object",
"properties": {
"label": {"type": "string"},
"host": {"type": "string"},
"port": {"type": "integer"},
"protocol": {"type": "string", "default": "http"},
"username": {"type": "string"},
"password": {"type": "string"},
"kind": {"type": "string", "default": "http"},
"metadata": {"type": "object"},
},
"required": ["label", "host", "port"],
},
),
Tool(
name="conduit_marketplace_list_proxies",
description="List named marketplace proxy routes.",
inputSchema={
"type": "object",
"properties": {
"state": {"type": "string", "description": "Optional proxy state filter"},
},
},
),
Tool(
name="conduit_marketplace_get_proxy",
description="Fetch one named marketplace proxy route.",
inputSchema={
"type": "object",
"properties": {
"label": {"type": "string", "description": "Proxy label"},
},
"required": ["label"],
},
),
Tool(
name="conduit_marketplace_test_proxy",
description="Launch a temporary browser through a named proxy route and test a URL.",
inputSchema={
"type": "object",
"properties": {
"label": {"type": "string", "description": "Proxy label"},
"test_url": {"type": "string", "description": "Optional URL to test", "default": "https://api.ipify.org/"},
},
"required": ["label"],
},
),
Tool(
name="conduit_marketplace_save_session",
description="Register a saved marketplace session cookie file for an account.",
inputSchema={
"type": "object",
"properties": {
"account_id": {"type": "string"},
"label": {"type": "string"},
"cookie_path": {"type": "string"},
"state": {"type": "string"},
"metadata": {"type": "object"},
},
"required": ["account_id", "label", "cookie_path"],
},
),
Tool(
name="conduit_marketplace_list_accounts",
description="List configured marketplace accounts.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string"},
},
},
),
Tool(
name="conduit_marketplace_list_sessions",
description="List saved marketplace sessions.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string"},
"account_id": {"type": "string"},
},
},
),
Tool(
name="conduit_marketplace_get_session",
description="Fetch one saved marketplace session by id.",
inputSchema={
"type": "object",
"properties": {
"session_id": {"type": "string", "description": "Marketplace session id"},
},
"required": ["session_id"],
},
),
Tool(
name="conduit_marketplace_bootstrap_session",
description="Log into a marketplace account using its credential key and persist a fresh saved session.",
inputSchema={
"type": "object",
"properties": {
"account_id": {"type": "string", "description": "Marketplace account id"},
"target_url": {"type": "string", "description": "Optional target URL to bootstrap against"},
},
"required": ["account_id"],
},
),
Tool(
name="conduit_marketplace_run_job",
description="Execute a queued marketplace extraction job using Conduit's audited browser.",
inputSchema={
"type": "object",
"properties": {
"job_id": {"type": "string", "description": "Marketplace job id"},
},
"required": ["job_id"],
},
),
Tool(
name="conduit_marketplace_enqueue_job",
description="Queue a marketplace job for background execution in the current server process.",
inputSchema={
"type": "object",
"properties": {
"job_id": {"type": "string", "description": "Marketplace job id"},
},
"required": ["job_id"],
},
),
Tool(
name="conduit_marketplace_queue_status",
description="Inspect queued/running/completed marketplace jobs in the current server process.",
inputSchema={
"type": "object",
"properties": {
"job_id": {"type": "string", "description": "Optional marketplace job id"},
},
},
),
Tool(
name="conduit_marketplace_get_job",
description="Fetch one marketplace extraction job by id.",
inputSchema={
"type": "object",
"properties": {
"job_id": {"type": "string", "description": "Marketplace job id"},
},
"required": ["job_id"],
},
),
Tool(
name="conduit_marketplace_list_jobs",
description="List marketplace extraction jobs.",
inputSchema={
"type": "object",
"properties": {
"marketplace": {"type": "string", "description": "Optional marketplace filter"},
"status": {"type": "string", "description": "Optional status filter"},
},
},
),
Tool(
name="conduit_marketplace_list_results",
description="List persisted marketplace extraction results.",
inputSchema={
"type": "object",
"properties": {
"job_id": {"type": "string", "description": "Optional job id filter"},
},
},
),
Tool(
name="conduit_marketplace_get_result",
description="Fetch one persisted marketplace extraction result by id.",
inputSchema={
"type": "object",
"properties": {
"result_id": {"type": "string", "description": "Marketplace result id"},
},
"required": ["result_id"],
},
),
Tool(
name="conduit_marketplace_export_result",
description="Export one marketplace result as JSONL or CSV.",
inputSchema={
"type": "object",
"properties": {
"result_id": {"type": "string", "description": "Marketplace result id"},
"fmt": {"type": "string", "description": "Export format: jsonl or csv", "default": "jsonl"},
},
"required": ["result_id"],
},
),
]
# ---------------------------------------------------------------------------
# Action dispatch
# ---------------------------------------------------------------------------
async def _dispatch(tool_name: str, arguments: dict) -> str:
bridge = await _get_bridge()
action_map = {
"conduit_navigate": "navigate",
"conduit_screenshot": "screenshot",
"conduit_extract": "extract",
"conduit_click": "click",
"conduit_fill": "fill",
"conduit_search": "search",
"conduit_eval": "eval",
"conduit_export_proof": "export_proof",
"conduit_marketplace_list": "marketplace_list",
"conduit_marketplace_targets": "marketplace_targets",
"conduit_marketplace_plan": "marketplace_plan",
"conduit_marketplace_create_job": "marketplace_create_job",
"conduit_marketplace_create_account": "marketplace_create_account",
"conduit_marketplace_create_proxy": "marketplace_create_proxy",
"conduit_marketplace_list_proxies": "marketplace_list_proxies",
"conduit_marketplace_get_proxy": "marketplace_get_proxy",
"conduit_marketplace_test_proxy": "marketplace_test_proxy",
"conduit_marketplace_save_session": "marketplace_save_session",
"conduit_marketplace_list_accounts": "marketplace_list_accounts",
"conduit_marketplace_list_sessions": "marketplace_list_sessions",
"conduit_marketplace_get_session": "marketplace_get_session",
"conduit_marketplace_bootstrap_session": "marketplace_bootstrap_session",
"conduit_marketplace_run_job": "marketplace_execute_job",
"conduit_marketplace_enqueue_job": "marketplace_enqueue_job",
"conduit_marketplace_queue_status": "marketplace_queue_status",
"conduit_marketplace_get_job": "marketplace_get_job",
"conduit_marketplace_list_jobs": "marketplace_list_jobs",
"conduit_marketplace_list_results": "marketplace_list_results",
"conduit_marketplace_get_result": "marketplace_get_result",
"conduit_marketplace_export_result": "marketplace_export_result",
}
action = action_map.get(tool_name)
if action is None:
return json.dumps({"error": f"Unknown tool: {tool_name}"})
args = {"action": action, **arguments}
try:
result = await bridge.execute(args)
if isinstance(result, str):
return result
return json.dumps(result, ensure_ascii=False)
except Exception as exc:
return json.dumps({"error": str(exc)})
# ---------------------------------------------------------------------------
# Server setup
# ---------------------------------------------------------------------------
def create_server() -> Server:
server = Server("conduit-browser")
@server.list_tools()
async def list_tools() -> list[Tool]:
return TOOLS
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
result = await _dispatch(name, arguments or {})
return [TextContent(type="text", text=result)]
return server
async def main():
server = create_server()
async with stdio_server() as (read_stream, write_stream):
await server.run(read_stream, write_stream, server.create_initialization_options())
if __name__ == "__main__":
asyncio.run(main())