From 15d3d0e961eabfc0da0d533c4dc3d3f63572ed53 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Tue, 8 Jul 2025 13:47:15 +0100 Subject: [PATCH] - Added MCP tool invocation to API with flow-specific 'mcp_tool' method. - Added tg-invoke-mcp-tool to make it easy to test the MCP integration part without using an agent flow. --- trustgraph-base/trustgraph/api/flow.py | 29 +++++++- trustgraph-cli/scripts/tg-invoke-mcp-tool | 80 +++++++++++++++++++++++ trustgraph-cli/setup.py | 1 + 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100755 trustgraph-cli/scripts/tg-invoke-mcp-tool diff --git a/trustgraph-base/trustgraph/api/flow.py b/trustgraph-base/trustgraph/api/flow.py index 8c872fd1..61873e99 100644 --- a/trustgraph-base/trustgraph/api/flow.py +++ b/trustgraph-base/trustgraph/api/flow.py @@ -4,6 +4,7 @@ import base64 from .. knowledge import hash, Uri, Literal from . types import Triple +from . exceptions import ProtocolException def to_value(x): if x["e"]: return Uri(x["v"]) @@ -197,7 +198,6 @@ class FlowInstance: def prompt(self, id, variables): - # The input consists of system and prompt strings input = { "id": id, "variables": variables @@ -221,12 +221,37 @@ class FlowInstance: raise ProtocolException("Response not formatted correctly") + def mcp_tool(self, name, parameters={}): + + # The input consists of name and parameters + input = { + "name": name, + "parameters": parameters, + } + + object = self.request( + "service/mcp-tool", + input + ) + + if "text" in object: + return object["text"] + + if "object" in object: + try: + return object["object"] + except Exception as e: + raise ProtocolException( + "Returned object not well-formed JSON" + ) + + raise ProtocolException("Response not formatted correctly") + def triples_query( self, s=None, p=None, o=None, user=None, collection=None, limit=10000 ): - # The input consists of system and prompt strings input = { "limit": limit } diff --git a/trustgraph-cli/scripts/tg-invoke-mcp-tool b/trustgraph-cli/scripts/tg-invoke-mcp-tool new file mode 100755 index 00000000..e5fb148f --- /dev/null +++ b/trustgraph-cli/scripts/tg-invoke-mcp-tool @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +""" +Invokes MCP (Model Control Protocol) tools through the TrustGraph API. +Allows calling MCP tools by specifying the tool name and providing +parameters as a JSON-encoded dictionary. The tool is executed within +the context of a specified flow. +""" + +import argparse +import os +import json +from trustgraph.api import Api + +default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/') + +def query(url, flow_id, name, parameters): + + api = Api(url).flow().id(flow_id) + + resp = api.mcp_tool(name=name, parameters=parameters) + + if isinstance(resp, str): + print(resp) + else: + print(json.dumps(resp, indent=4)) + +def main(): + + parser = argparse.ArgumentParser( + prog='tg-invoke-mcp-tool', + description=__doc__, + ) + + parser.add_argument( + '-u', '--url', + default=default_url, + help=f'API URL (default: {default_url})', + ) + + parser.add_argument( + '-f', '--flow-id', + default="default", + help=f'Flow ID (default: default)' + ) + + parser.add_argument( + '-n', '--name', + metavar='tool-name', + help=f'MCP tool name', + ) + + parser.add_argument( + '-P', '--parameters', + help='''Tool parameters, should be JSON-encoded dict.''', + ) + + args = parser.parse_args() + + + if args.parameters: + parameters = json.loads(args.parameters) + else: + parameters = {} + + try: + + query( + url = args.url, + flow_id = args.flow_id, + name = args.name, + parameters = parameters, + ) + + except Exception as e: + + print("Exception:", e, flush=True) + +main() + diff --git a/trustgraph-cli/setup.py b/trustgraph-cli/setup.py index cd961c2d..6af3f38e 100644 --- a/trustgraph-cli/setup.py +++ b/trustgraph-cli/setup.py @@ -56,6 +56,7 @@ setuptools.setup( "scripts/tg-invoke-document-rag", "scripts/tg-invoke-graph-rag", "scripts/tg-invoke-llm", + "scripts/tg-invoke-mcp-tool", "scripts/tg-invoke-prompt", "scripts/tg-load-doc-embeds", "scripts/tg-load-kg-core",