Skip to content

Commit cab4d53

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 2464c45 commit cab4d53

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

mcpserver/core/hub.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ def _print_banner(self):
125125
print(" Workers must use this secret to join the hub")
126126
print(f" mcpserver start --join {self.registration_url}\n")
127127

128+
def jsonify_response(self, response):
129+
"""
130+
Ensure we get the text, and separate and parse tool calls,
131+
which the agent will return in a verbose mode.
132+
"""
133+
result = result.content[0].text
134+
135+
# Audit the tool calls (Did the agent just get lucky?)
136+
calls = []
137+
if "CALLS" in result:
138+
try:
139+
result, calls_block = result.split("CALLS")
140+
calls = utils.format_calls(calls_block)
141+
except:
142+
print(f"Issue parsing calls, agent had malformed response: {result}")
143+
pass
144+
145+
result = json.loads(utils.extract_code_block(result))
146+
result["calls"] = calls
147+
return result
148+
128149
async def run_on_fleet_parallel(self, action_fn) -> Dict[str, Any]:
129150
"""
130151
Run parallel sessions across all workers.
@@ -209,7 +230,7 @@ async def dispatch_job(worker_id: str, prompt: str) -> dict:
209230

210231
async with info["client"] as sess:
211232
result = await sess.call_tool("submit", {"request": prompt})
212-
return json.loads(utils.extract_code_block(result.content[0].text))
233+
return self.jsonify_response(result)
213234

214235
@self.mcp.tool(name="negotiate_job")
215236
async def negotiate_job(prompt: str) -> dict:
@@ -253,6 +274,7 @@ async def negotiate_handler(wid, sess):
253274
mcp_result = await sess.call_tool("ask_secretary", {"request": prompt})
254275
raw_text = mcp_result.content[0].text
255276

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

mcpserver/utils/text.py

Lines changed: 14 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,16 @@ 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+
return json.loads(extract_code_block(calls_block))
25+
except:
26+
return calls
1527

1628

1729
def extract_code_block(text):

0 commit comments

Comments
 (0)