From 1f1e7b1940b65f8a6f27cb4c9ed8ebfc3a4f9df0 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Mon, 7 Jul 2025 23:40:46 +0100 Subject: [PATCH] Make mcp-tool using configuration service for information about where the MCP services are. --- .../trustgraph/agent/mcp_tool/service.py | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/trustgraph-flow/trustgraph/agent/mcp_tool/service.py b/trustgraph-flow/trustgraph/agent/mcp_tool/service.py index 81fe0717..b20f26b5 100755 --- a/trustgraph-flow/trustgraph/agent/mcp_tool/service.py +++ b/trustgraph-flow/trustgraph/agent/mcp_tool/service.py @@ -4,11 +4,12 @@ MCP tool-calling service, calls an external MCP tool. Input is name + parameters, output is the response, either a string or an object. """ -from ... base import ToolService - +import json from mcp.client.streamable_http import streamablehttp_client from mcp import ClientSession +from ... base import ToolService + default_ident = "mcp-tool" class Service(ToolService): @@ -19,14 +20,42 @@ class Service(ToolService): **params ) + self.register_config_handler(self.on_mcp_config) + + self.mcp_services = {} + + async def on_mcp_config(self, config, version): + + print("Got config version", version) + + if "mcp" not in config: return + + self.mcp_services = { + k: json.loads(v) + for k, v in config["mcp"].items() + } + async def invoke_tool(self, name, parameters): try: - print(name) - print(parameters) + + if name not in self.mcp_services: + raise RuntimeError(f"MCP service {name} not known") + + if "url" not in self.mcp_services[name]: + raise RuntimeError(f"MCP service {name} URL not defined") + + url = self.mcp_services[name]["url"] + + if "name" in self.mcp_services[name]: + remote_name = self.mcp_services[name]["name"] + else: + remote_name = name + + print("Invoking", remote_name, "at", url, flush=True) + # Connect to a streamable HTTP server -# async with streamablehttp_client("http://mcp-server:8000/mcp/") as ( - async with streamablehttp_client("http://host.containers.internal:9870/mcp/") as ( + async with streamablehttp_client(url) as ( read_stream, write_stream, _, @@ -40,12 +69,10 @@ class Service(ToolService): # Call a tool result = await session.call_tool( - name, + remote_name, parameters ) - print(result) - if result.structuredContent: return result.structuredContent elif hasattr(result, "content"):