Skip to content

Commit 1249252

Browse files
committed
load custom toolsets
1 parent 47ac425 commit 1249252

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

packages/toolbox-core/src/mcp_poc/mcp_transport.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from abc import ABC, abstractmethod
2-
from typing import Dict
2+
from typing import Optional, Dict
33
import httpx
44

55
class ITransport(ABC):
66
@abstractmethod
7-
async def tools_list(self) -> Dict:
7+
async def tools_list(self, toolset_name: Optional[str] = None) -> Dict:
88
pass
99

1010
@abstractmethod
@@ -25,16 +25,26 @@ def _build_json_rpc_payload(self, method: str, params: Dict) -> Dict:
2525
self._request_id += 1
2626
return {"jsonrpc": "2.0", "method": method, "params": params, "id": self._request_id}
2727

28-
async def tools_list(self) -> Dict:
28+
def _get_list_endpoint(self, toolset_name: Optional[str] = None) -> str:
29+
"""Constructs the correct API endpoint for listing tools."""
30+
if toolset_name:
31+
return f"{self._base_url}/mcp/{toolset_name}"
32+
return f"{self._base_url}/mcp"
33+
34+
async def tools_list(self, toolset_name: Optional[str] = None) -> Dict:
35+
"""Lists tools from the default endpoint or a specific toolset."""
36+
endpoint = self._get_list_endpoint(toolset_name)
2937
payload = self._build_json_rpc_payload("tools/list", {})
30-
response = await self._client.post(f"{self._base_url}/mcp", json=payload)
38+
response = await self._client.post(endpoint, json=payload)
3139
response.raise_for_status()
3240
return response.json()
3341

3442
async def tool_invoke(self, tool_name: str, arguments: Dict) -> Dict:
43+
"""Invokes a tool using the global /mcp endpoint."""
44+
endpoint = f"{self._base_url}/mcp"
3545
params = {"name": tool_name, "arguments": arguments}
3646
payload = self._build_json_rpc_payload("tools/call", params)
37-
response = await self._client.post(f"{self._base_url}/mcp", json=payload)
47+
response = await self._client.post(endpoint, json=payload)
3848
response.raise_for_status()
3949
return response.json()
4050

packages/toolbox-core/src/mcp_poc/run.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import asyncio
22
import json
33
from mcp_transport import McpHttpTransport
4+
from typing import Optional
45

56
class MCPClient:
67
"""A simple client to interact with an MCP server."""
78
def __init__(self, base_url: str):
89
self._transport = McpHttpTransport(base_url=base_url)
910

10-
async def list_all_tools(self):
11-
print("--> Attempting to list tools...")
12-
response = await self._transport.tools_list()
11+
async def list_tools(self, toolset_name: Optional[str] = None):
12+
"""Lists tools, either all or from a specific toolset."""
13+
if toolset_name:
14+
print(f"--> Attempting to list tools from toolset: '{toolset_name}'...")
15+
else:
16+
print("--> Attempting to list all tools...")
17+
18+
response = await self._transport.tools_list(toolset_name=toolset_name)
1319
return response.get("result", {}).get("tools", [])
1420

15-
async def invoke_a_tool(self, tool_name: str, args: dict):
21+
async def invoke_tool(self, tool_name: str, args: dict):
22+
"""Invokes a tool using the global endpoint."""
1623
print(f"\n--> Attempting to invoke tool: '{tool_name}'...")
1724
response = await self._transport.tool_invoke(tool_name, args)
25+
26+
1827
return response.get("result", {})
1928

2029
async def close(self):
@@ -27,14 +36,20 @@ async def main():
2736

2837
try:
2938
# 1. List all available tools
30-
tools = await client.list_all_tools()
31-
print("\n✅ Tools listed successfully:")
32-
print(json.dumps(tools, indent=2))
39+
all_tools = await client.list_tools()
40+
print("\n✅ All tools listed successfully:")
41+
print(json.dumps(all_tools, indent=2))
42+
43+
# 2. List tools from a specific toolset
44+
custom_toolset_name = "my-toolset-2"
45+
custom_tools = await client.list_tools(toolset_name=custom_toolset_name)
46+
print(f"\n✅ Tools from '{custom_toolset_name}' toolset listed successfully:")
47+
print(json.dumps(custom_tools, indent=2))
3348

34-
# 2. Invoke a specific tool
49+
# 3. Invoke a tool. This correctly uses the global endpoint.
3550
tool_to_invoke = "get-n-rows"
3651
arguments = {"num_rows": "2"}
37-
invocation_result = await client.invoke_a_tool(tool_to_invoke, arguments)
52+
invocation_result = await client.invoke_tool(tool_to_invoke, arguments)
3853

3954
print("\n✅ Tool invoked successfully:")
4055
print(json.dumps(invocation_result, indent=2))

0 commit comments

Comments
 (0)