Aniruddha7 commited on
Commit
ef66844
·
verified ·
1 Parent(s): 1c4faa7

Restore files the worker imports (parity with original Space)

Browse files
Files changed (1) hide show
  1. Agent/mcp_client.py +48 -0
Agent/mcp_client.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Minimal MCP client for local MCP server.
2
+
3
+ Provides mcp_complete(prompt, timeout, context) -> str
4
+ Tries to POST to MCP server and return the completion text. Raises on errors so caller can fallback.
5
+ """
6
+ import os
7
+ import json
8
+ import requests
9
+ from typing import Any, Dict, Optional
10
+
11
+ MCP_URL = os.environ.get("MCP_URL", "http://127.0.0.1:8701/complete")
12
+
13
+
14
+ def mcp_complete(prompt: str, timeout: float = 30.0, context: Optional[Dict[str, Any]] = None) -> str:
15
+ """Send a completion request to the local MCP server and return text.
16
+
17
+ Raises requests.RequestException on transport errors or ValueError if MCP returns error.
18
+ """
19
+ payload = {
20
+ "prompt": prompt,
21
+ "context": context or {},
22
+ "timeout": timeout
23
+ }
24
+ headers = {"Content-Type": "application/json"}
25
+ resp = requests.post(MCP_URL, data=json.dumps(payload), headers=headers, timeout=timeout)
26
+ resp.raise_for_status()
27
+ body = resp.json()
28
+ # Expecting {'text': '...'} from MCP server
29
+ if not isinstance(body, dict) or 'text' not in body:
30
+ raise ValueError(f"Unexpected MCP response: {body}")
31
+ return body['text']
32
+
33
+
34
+ def mcp_call_tool(prompt: str, timeout: float = 60.0, context: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
35
+ """Call the MCP /complete endpoint and return the full JSON response.
36
+
37
+ This is useful when asking MCP to run a registered tool; the tool result will
38
+ appear under the `tool_result` key in the returned dict.
39
+ """
40
+ payload = {
41
+ "prompt": prompt,
42
+ "context": context or {},
43
+ "timeout": timeout,
44
+ }
45
+ headers = {"Content-Type": "application/json"}
46
+ resp = requests.post(MCP_URL, data=json.dumps(payload), headers=headers, timeout=timeout)
47
+ resp.raise_for_status()
48
+ return resp.json()