mirror of
https://github.com/NVIDIA/TensorRT-LLM.git
synced 2026-02-06 19:21:52 +08:00
* support mcp # Conflicts: # tensorrt_llm/scaffolding/worker.py Signed-off-by: wu1du2 <wu1du2@gmail.com> * move all into contrib/mcp # Conflicts: # examples/scaffolding/contrib/mcp/mcptest.py # tensorrt_llm/scaffolding/__init__.py # tensorrt_llm/scaffolding/contrib/__init__.py # tensorrt_llm/scaffolding/contrib/mcp/__init__.py # tensorrt_llm/scaffolding/contrib/mcp/mcp_controller.py # tensorrt_llm/scaffolding/task.py # tensorrt_llm/scaffolding/worker.py Signed-off-by: wu1du2 <wu1du2@gmail.com> * support sandbox, websearch # Conflicts: # examples/scaffolding/contrib/mcp/mcptest.py # examples/scaffolding/contrib/mcp/weather/weather.py # tensorrt_llm/scaffolding/contrib/mcp/mcp_controller.py # tensorrt_llm/scaffolding/contrib/mcp/mcp_utils.py # tensorrt_llm/scaffolding/contrib/mcp/mcp_worker.py # tensorrt_llm/scaffolding/worker.py Signed-off-by: wu1du2 <wu1du2@gmail.com> * remove pics Signed-off-by: wu1du2 <wu1du2@gmail.com> * pre-commit fix # Conflicts: # tensorrt_llm/scaffolding/contrib/mcp/__init__.py # tensorrt_llm/scaffolding/contrib/mcp/mcp_utils.py # tensorrt_llm/scaffolding/contrib/mcp/mcp_worker.py Signed-off-by: wu1du2 <wu1du2@gmail.com> * fix spell Signed-off-by: wu1du2 <wu1du2@gmail.com> * rebase Signed-off-by: wu1du2 <wu1du2@gmail.com> --------- Signed-off-by: wu1du2 <wu1du2@gmail.com>
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
import asyncio
|
|
from contextlib import AsyncExitStack
|
|
from typing import Optional
|
|
|
|
from dotenv import load_dotenv
|
|
from mcp import ClientSession
|
|
from mcp.client.sse import sse_client
|
|
|
|
load_dotenv() # load environment variables from .env
|
|
|
|
|
|
class MCPClient:
|
|
|
|
def __init__(self):
|
|
# Initialize session and client objects
|
|
self.session: Optional[ClientSession] = None
|
|
self.exit_stack = AsyncExitStack()
|
|
|
|
async def list_tools(self):
|
|
response = await self.session.list_tools()
|
|
return response
|
|
|
|
async def call_tool(self, tool_name, tool_args):
|
|
result = await self.session.call_tool(tool_name, tool_args)
|
|
return result
|
|
|
|
async def connect_to_sse_server(self, server_url: str):
|
|
"""Connect to an MCP server running with SSE transport"""
|
|
streams_context = sse_client(url=server_url)
|
|
streams = await self.exit_stack.enter_async_context(streams_context)
|
|
|
|
session_context = ClientSession(*streams)
|
|
self.session = await self.exit_stack.enter_async_context(session_context
|
|
)
|
|
|
|
# Initialize session
|
|
await self.session.initialize()
|
|
|
|
async def cleanup(self):
|
|
"""Properly clean up all registered async resources."""
|
|
await self.exit_stack.aclose()
|
|
|
|
|
|
async def main():
|
|
if len(sys.argv) < 2:
|
|
print(
|
|
"Usage: uv run client.py <URL of SSE MCP server (i.e. http://localhost:8080/sse)>"
|
|
)
|
|
sys.exit(1)
|
|
|
|
client = MCPClient()
|
|
try:
|
|
await client.connect_to_sse_server(server_url=sys.argv[1])
|
|
finally:
|
|
await client.cleanup()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
asyncio.run(main())
|