Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: pass Codex MCP and sub-agent tools to the model on /v1/responses (#13483)

Nilay03500340fa77
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 28ea9bc8c18..b0eb9376bca 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -3684,6 +3684,7 @@ class ResponsesFunctionCallInputItem(BaseModel):
description = "Correlation id matching a function_call_output on the next turn.",
)
name: str
+ namespace: Optional[str] = None
arguments: str = Field(..., description = "JSON string of the arguments the model produced.")
status: Optional[Literal["in_progress", "completed", "incomplete"]] = None
@@ -3885,9 +3886,17 @@ class ResponsesOutputFunctionCall(BaseModel):
id: str = Field(default_factory = lambda: f"fc_{uuid.uuid4().hex[:12]}")
call_id: str
name: str
+ namespace: Optional[str] = None
arguments: str = Field(..., description = "JSON string of the arguments the model produced.")
status: Literal["completed", "in_progress", "incomplete"] = "completed"
+ @model_serializer(mode = "wrap")
+ def _omit_unset_namespace(self, handler):
+ data = handler(self)
+ if isinstance(data, dict) and data.get("namespace") is None:
+ data.pop("namespace", None)
+ return data
+
class ResponsesOutputCustomToolCall(BaseModel):
"""A freeform custom-tool call returned by the Responses API."""
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 0f8ba1e72f1..3b7eca1feee 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -37936,6 +37936,41 @@ def _responses_custom_tool_names(tools: Optional[list[dict]]) -> set[str]:
return {tool["name"] for tool in tools or [] if _is_responses_apply_patch_tool(tool)}
+def _responses_namespaced_name(namespace: str, name: str) -> str:
+ return f"{namespace}__{name}"
+
+
+def _responses_namespace_functions(tool: dict) -> list[tuple[str, dict]]:
+ namespace = tool.get("name")
+ if not isinstance(namespace, str) or not namespace:
+ return []
+ return [
+ (_responses_namespaced_name(namespace, inner["name"]), inner)
+ for inner in tool.get("tools") or []
+ if isinstance(inner, dict)
+ and inner.get("type") == "function"
+ and isinstance(inner.get("name"), str)
+ ]
+
+
+def _responses_namespaced_tool_names(tools: Optional[list[dict]]) -> dict[str, tuple[str, str]]:
+ return {
+ flat_name: (tool["name"], inner["name"])
+ for tool in tools or []
+ if isinstance(tool, dict) and tool.get("type") == "namespace"
+ for flat_name, inner in _responses_namespace_functions(tool)
+ }
+
+
+def _responses_function_call_name(
+ name: str, namespaced_tool_names: dict[str, tuple[str, str]]
+) -> dict:
+ if name in namespaced_tool_names:
+ namespace, bare_name = namespaced_tool_names[name]
+ return {"name": bare_name, "namespace": namespace}
+ return {"name": name}
+
+
def _responses_apply_patch_description(tool: dict) -> str:
description = (
"Edit files by passing one complete patch in the input field. "
@@ -37998,21 +38033,31 @@ def _translate_responses_tools_to_chat(tools: Optional[list[dict]]) -> Optional[
}
)
continue
+ if tool.get("type") == "namespace":
+ out.extend(
+ _responses_function_tool_to_chat({**inner, "name": flat_name})
+ for flat_name, inner in _responses_namespace_functions(tool)
+ )
+ continue
if tool.get("type") != "function":
continue
- fn: dict = {}
- if "name" in tool:
- fn["name"] = tool["name"]
- if tool.get("description") is not None:
- fn["description"] = tool["description"]
- if tool.get("parameters") is not None:
- fn["parameters"] = tool["parameters"]
- if tool.get("strict") is not None:
- fn["strict"] = tool["strict"]
- out.append({"type": "function", "function": fn})
+ out.append(_responses_function_tool_to_chat(tool))
return out or None
+def _responses_function_tool_to_chat(tool: dict) -> dict:
+ fn: dict = {}
+ if "name" in tool:
+ fn["name"] = tool["name"]
+ if tool.get("description") is not None:
+ fn["description"] = tool["description"]
+ if tool.get("parameters") is not None:
+ fn["parameters"] = tool["parameters"]
+ if tool.get("strict") is not None:
+ fn["strict"] = tool["strict"]
+ return {"type": "function", "function": fn}
+
+
def _translate_responses_tool_choice_to_chat(
tool_choice: Any, custom_tool_names: Optional[set[str]] = None
) -> Any:
@@ -38507,7 +38552,11 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:
"id": item.call_id,
"type": "function",
"function": {
- "name": item.name,
+ "name": (
+ _responses_namespaced_name(item.namespace, item.name)
+ if item.namespace
+ else item.name
+ ),
"arguments": item.arguments,
},
}
@@ -38734,6 +38783,7 @@ def _chat_tool_calls_to_responses_output(
tool_calls: list[dict],
custom_tool_names: Optional[set[str]] = None,
status: Literal["completed", "incomplete"] = "completed",
+ namespaced_tool_names: Optional[dict[str, tuple[str, str]]] = None,
) -> list[dict]:
"""Map local function calls back into their Responses output item types.
@@ -38762,7 +38812,7 @@ def _chat_tool_calls_to_responses_output(
items.append(
ResponsesOutputFunctionCall(
call_id = tc.get("id", ""),
- name = name,
+ **_responses_function_call_name(name, namespaced_tool_names or {}),
arguments = arguments,
status = status,
).model_dump()
@@ -38884,6 +38934,7 @@ async def _responses_non_streaming(
tool_calls,
_responses_custom_tool_names(payload.tools),
status = item_status,
+ namespaced_tool_names = _responses_namespaced_tool_names(payload.tools),
)
)
@@ -38971,6 +39022,7 @@ async def _responses_stream(
chat_req = _build_chat_request(payload, messages, stream = True)
custom_tool_names = _responses_custom_tool_names(payload.tools)
+ namespaced_tool_names = _responses_namespaced_tool_names(payload.tools)
llama_backend = get_llama_cpp_backend()
if not llama_backend.is_loaded:
@@ -39182,7 +39234,7 @@ async def _responses_stream(
"id": st["item_id"],
"status": "in_progress",
"call_id": st["call_id"],
- "name": st["name"],
+ **_responses_function_call_name(st["name"], namespaced_tool_names),
"arguments": "",
},
}
@@ -39434,7 +39486,7 @@ async def _responses_stream(
"id": st["item_id"],
"status": active_item_status,
"call_id": st["call_id"],
- "name": st["name"],
+ **_responses_function_call_name(st["name"], namespaced_tool_names),
"arguments": st["arguments"],
}
indexed_items.append(
@@ -39922,7 +39974,7 @@ async def _responses_stream(
"id": st["item_id"],
"status": "in_progress",
"call_id": st["call_id"],
- "name": st["name"],
+ **_responses_function_call_name(st["name"], namespaced_tool_names),
"arguments": "",
},
}
@@ -39943,7 +39995,7 @@ async def _responses_stream(
"type": "response.function_call_arguments.done",
"item_id": st["item_id"],
"output_index": st["output_index"],
- "name": st["name"],
+ **_responses_function_call_name(st["name"], namespaced_tool_names),
"arguments": st["arguments"],
}
yield _sse("response.function_call_arguments.done", args_done)
@@ -39956,7 +40008,7 @@ async def _responses_stream(
"id": st["item_id"],
"status": active_item_status,
"call_id": st["call_id"],
- "name": st["name"],
+ **_responses_function_call_name(st["name"], namespaced_tool_names),
"arguments": st["arguments"],
},
}
diff --git a/studio/backend/tests/test_responses_tool_passthrough.py b/studio/backend/tests/test_responses_tool_passthrough.py
index 9f10dafec6d..d4a2add6a5d 100644
--- a/studio/backend/tests/test_responses_tool_passthrough.py
+++ b/studio/backend/tests/test_responses_tool_passthrough.py
@@ -144,6 +144,63 @@ def _codex_apply_patch_tool():
}
+def _codex_namespace_tools():
+ forecast_params = {
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ }
+ return [
+ {"type": "function", "name": "exec_command", "parameters": {"type": "object"}},
+ {
+ "type": "namespace",
+ "name": "multi_agent_v1",
+ "description": "Tools for spawning and managing sub-agents.",
+ "tools": [
+ {"type": "function", "name": "spawn_agent", "parameters": {"type": "object"}},
+ {"type": "function", "name": "wait_agent", "parameters": {"type": "object"}},
+ ],
+ },
+ {
+ "type": "namespace",
+ "name": "mcp__weather",
+ "description": "Tools in the mcp__weather namespace.",
+ "tools": [
+ {
+ "type": "function",
+ "name": "get_forecast",
+ "description": "Get the weather forecast for a city.",
+ "strict": False,
+ "parameters": forecast_params,
+ }
+ ],
+ },
+ {"type": "web_search"},
+ ]
+
+
+def _namespaced_forecast_call_chunk():
+ return {
+ "choices": [
+ {
+ "delta": {
+ "tool_calls": [
+ {
+ "index": 0,
+ "id": "call_wx",
+ "type": "function",
+ "function": {
+ "name": "mcp__weather__get_forecast",
+ "arguments": '{"city":"Paris"}',
+ },
+ }
+ ]
+ }
+ }
+ ]
+ }
+
+
# =====================================================================
# Request model — tools / tool_choice / parallel_tool_calls
# =====================================================================
@@ -320,6 +377,26 @@ class TestToolsTranslation:
assert len(out) == 1
assert out[0]["function"]["name"] == "search"
+ def test_codex_namespace_tools_reach_the_model(self):
+ out = _translate_responses_tools_to_chat(_codex_namespace_tools())
+
+ assert [tool["function"]["name"] for tool in out] == [
+ "exec_command",
+ "multi_agent_v1__spawn_agent",
+ "multi_agent_v1__wait_agent",
+ "mcp__weather__get_forecast",
+ ]
+ assert out[3]["function"] == {
+ "name": "mcp__weather__get_forecast",
+ "description": "Get the weather forecast for a city.",
+ "parameters": {
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ },
+ "strict": False,
+ }
+
def test_codex_apply_patch_custom_tool_becomes_a_local_function(self):
grammar = (
'start: "*** Begin Patch" LF add_hunk "*** End Patch" LF?\n'
@@ -639,6 +716,27 @@ class TestNormaliseResponsesInputWithTools:
assert msgs[2].tool_call_id == "call_1"
assert msgs[2].content == '{"temp": 20}'
+ def test_replayed_namespaced_call_keeps_the_name_the_model_called(self):
+ payload = ResponsesRequest(
+ input = [
+ {"role": "user", "content": "Weather in Paris?"},
+ {
+ "type": "function_call",
+ "call_id": "call_wx",
+ "namespace": "mcp__weather",
+ "name": "get_forecast",
+ "arguments": '{"city":"Paris"}',
+ },
+ {"type": "function_call_output", "call_id": "call_wx", "output": "17C"},
+ ],
+ tools = _codex_namespace_tools(),
+ )
+
+ msgs = _normalise_responses_input(payload)
+
+ assert msgs[1].tool_calls[0]["function"]["name"] == "mcp__weather__get_forecast"
+ assert msgs[2].tool_call_id == "call_wx"
+
def test_custom_tool_call_round_trip_maps_to_local_function(self):
patch = "*** Begin Patch\n*** Add File: café.txt\n+héllo\n*** End Patch"
payload = ResponsesRequest(
@@ -1657,6 +1755,34 @@ class TestResponsesNonStreamingAdapter:
assert "<think>" not in body["output"][1]["content"][0]["text"]
assert "</think>" not in body["output"][1]["content"][0]["text"]
+ def test_namespaced_call_returns_to_its_namespace(self, monkeypatch):
+ payload = ResponsesRequest(input = "Weather in Paris?", tools = _codex_namespace_tools())
+ body = self._run_with_message(
+ monkeypatch,
+ {
+ "content": None,
+ "tool_calls": [
+ _namespaced_forecast_call_chunk()["choices"][0]["delta"]["tool_calls"][0],
+ {
+ "id": "call_ls",
+ "type": "function",
+ "function": {"name": "exec_command", "arguments": "{}"},
+ },
+ ],
+ },
+ payload = payload,
+ )
+
+ wx, ls = body["output"]
+ assert (wx["type"], wx["namespace"], wx["name"]) == (
+ "function_call",
+ "mcp__weather",
+ "get_forecast",
+ )
+ assert wx["call_id"] == "call_wx"
+ assert ls["name"] == "exec_command"
+ assert "namespace" not in ls
+
def test_apply_patch_function_becomes_a_custom_tool_call(self, monkeypatch):
patch = "*** Begin Patch\n*** Add File: nested/a.txt\n+ok\n*** End Patch"
payload = ResponsesRequest(
@@ -2685,6 +2811,29 @@ class TestResponsesStreamAdapter:
"message",
]
+ def test_streamed_namespaced_call_returns_to_its_namespace(self, monkeypatch):
+ self._install_stream_mock(monkeypatch, [_namespaced_forecast_call_chunk()])
+ payload = ResponsesRequest(
+ input = "Weather in Paris?", stream = True, tools = _codex_namespace_tools()
+ )
+ messages = [ChatMessage(role = "user", content = "Weather in Paris?")]
+
+ lines = self._stream_lines(payload, messages)
+
+ added = self._payloads(lines, "response.output_item.added")[0]["item"]
+ args_done = self._payloads(lines, "response.function_call_arguments.done")[0]
+ done = self._payloads(lines, "response.output_item.done")[0]["item"]
+ completed = self._payloads(lines, "response.completed")[0]["response"]["output"]
+ assert (args_done["namespace"], args_done["name"]) == ("mcp__weather", "get_forecast")
+ for item in (added, done, completed[0]):
+ assert (item["type"], item["namespace"], item["name"]) == (
+ "function_call",
+ "mcp__weather",
+ "get_forecast",
+ )
+ assert done["call_id"] == "call_wx"
+ assert done["arguments"] == '{"city":"Paris"}'
+
def test_apply_patch_stream_is_restored_to_one_custom_tool_call(self, monkeypatch):
patch = "*** Begin Patch\n*** Add File: nested/café.txt\n+héllo\n*** End Patch"
arguments = json.dumps({"input": patch}, ensure_ascii = False)