Skip to content

Commit ef3ed4f

Browse files
committed
hub: parse calls directly from dispatch
This will make it much easier for a user to see what was called. We can do this for negotiation too. Signed-off-by: vsoch <vsoch@users.noreply.github.com>
1 parent 30dd05b commit ef3ed4f

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

mcpserver/core/base.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ class WorkerBase:
1515
ask secretary. We provide it here so that a hub can use it to generate
1616
its dual mode (acting as worker AND hub.)
1717
"""
18+
def jsonify_response(self, result):
19+
"""
20+
Ensure we get the text, and separate and parse tool calls,
21+
which the agent will return in a verbose mode.
22+
"""
23+
print('result')
24+
print(result)
25+
print(type(result))
26+
if isinstance(result, dict):
27+
return result
28+
if not isinstance(result, str) and hasattr(result, "content"):
29+
result = result.content[0].text
30+
31+
# Audit the tool calls (Did the agent just get lucky?)
32+
calls = []
33+
if "CALLS" in result:
34+
try:
35+
result, calls_block = result.split("CALLS")
36+
calls = utils.format_calls(calls_block)
37+
except:
38+
print(f"Issue parsing calls, agent had malformed response: {result}")
39+
pass
40+
41+
result = json.loads(utils.extract_code_block(result))
42+
result["calls"] = calls
43+
return result
1844

1945
def init_providers(self, mock=False):
2046
"""
@@ -73,9 +99,9 @@ async def receive_job(request: str) -> dict:
7399
agent = SecretaryAgent(active_providers, verbose=self.verbose)
74100
raw_result = await agent.submit(request)
75101
try:
76-
receipt = json.loads(utils.extract_code_block(raw_result))
77-
except:
78-
receipt = {"status": "FAILED", "reasoning": raw_result}
102+
receipt = self.jsonify_response(raw_result)
103+
except Exception as e:
104+
receipt = {"status": "FAILED", "reasoning": raw_result, "error": str(e)}
79105

80106
return {"worker_id": self.worker_id, "receipt": receipt}
81107

mcpserver/core/hub.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ async def dispatch_job(worker_id: str, prompt: str) -> dict:
209209

210210
async with info["client"] as sess:
211211
result = await sess.call_tool("submit", {"request": prompt})
212-
return json.loads(utils.extract_code_block(result.content[0].text))
212+
print('response in hub')
213+
print(type(result))
214+
print(result)
215+
return self.jsonify_response(result)
213216

214217
@self.mcp.tool(name="negotiate_job")
215218
async def negotiate_job(prompt: str) -> dict:
@@ -253,6 +256,7 @@ async def negotiate_handler(wid, sess):
253256
mcp_result = await sess.call_tool("ask_secretary", {"request": prompt})
254257
raw_text = mcp_result.content[0].text
255258

259+
# TODO: vsoch: add support to parse the calls here too (like dispatch)
256260
try:
257261
# Parse and handle potential quote issues in LLM JSON
258262
proposal_data = json.loads(utils.extract_code_block(raw_text))

mcpserver/utils/text.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import json
12
import re
23

34

45
def sanitize(name: str) -> str:
6+
"""
7+
Sanitize worker ids and arguments for hub properties.
8+
"""
59
# Replace hyphens/dots with underscores
610
clean = name.replace("-", "_").replace(".", "_")
711
# Python identifiers cannot start with a digit
@@ -10,8 +14,21 @@ def sanitize(name: str) -> str:
1014
return clean
1115

1216

13-
def format_rules(rules):
14-
return "\n".join([f"- {r}" for r in rules])
17+
def format_calls(calls_block):
18+
"""
19+
The secretary agent can return calls. We need to ensure we try
20+
to get and parse them correctly.
21+
"""
22+
calls = []
23+
try:
24+
print(calls_block)
25+
print(type(calls_block))
26+
calls = extract_code_block(calls_block)
27+
print('success to extract calls')
28+
return calls
29+
except Exception as e:
30+
print(f'Issue in format calls: {e}')
31+
return calls
1532

1633

1734
def extract_code_block(text):

0 commit comments

Comments
 (0)