1
1
import asyncio
2
2
import json
3
3
from mcp_transport import McpHttpTransport
4
+ from typing import Optional
4
5
5
6
class MCPClient :
6
7
"""A simple client to interact with an MCP server."""
7
8
def __init__ (self , base_url : str ):
8
9
self ._transport = McpHttpTransport (base_url = base_url )
9
10
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 )
13
19
return response .get ("result" , {}).get ("tools" , [])
14
20
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."""
16
23
print (f"\n --> Attempting to invoke tool: '{ tool_name } '..." )
17
24
response = await self ._transport .tool_invoke (tool_name , args )
25
+
26
+
18
27
return response .get ("result" , {})
19
28
20
29
async def close (self ):
@@ -27,14 +36,20 @@ async def main():
27
36
28
37
try :
29
38
# 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 ))
33
48
34
- # 2 . Invoke a specific tool
49
+ # 3 . Invoke a tool. This correctly uses the global endpoint.
35
50
tool_to_invoke = "get-n-rows"
36
51
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 )
38
53
39
54
print ("\n ✅ Tool invoked successfully:" )
40
55
print (json .dumps (invocation_result , indent = 2 ))
0 commit comments