- 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.
This commit is contained in:
Cyber MacGeddon 2025-07-08 13:47:15 +01:00
parent 3978d35545
commit 15d3d0e961
3 changed files with 108 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import base64
from .. knowledge import hash, Uri, Literal from .. knowledge import hash, Uri, Literal
from . types import Triple from . types import Triple
from . exceptions import ProtocolException
def to_value(x): def to_value(x):
if x["e"]: return Uri(x["v"]) if x["e"]: return Uri(x["v"])
@ -197,7 +198,6 @@ class FlowInstance:
def prompt(self, id, variables): def prompt(self, id, variables):
# The input consists of system and prompt strings
input = { input = {
"id": id, "id": id,
"variables": variables "variables": variables
@ -221,12 +221,37 @@ class FlowInstance:
raise ProtocolException("Response not formatted correctly") 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( def triples_query(
self, s=None, p=None, o=None, self, s=None, p=None, o=None,
user=None, collection=None, limit=10000 user=None, collection=None, limit=10000
): ):
# The input consists of system and prompt strings
input = { input = {
"limit": limit "limit": limit
} }

View file

@ -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()

View file

@ -56,6 +56,7 @@ setuptools.setup(
"scripts/tg-invoke-document-rag", "scripts/tg-invoke-document-rag",
"scripts/tg-invoke-graph-rag", "scripts/tg-invoke-graph-rag",
"scripts/tg-invoke-llm", "scripts/tg-invoke-llm",
"scripts/tg-invoke-mcp-tool",
"scripts/tg-invoke-prompt", "scripts/tg-invoke-prompt",
"scripts/tg-load-doc-embeds", "scripts/tg-load-doc-embeds",
"scripts/tg-load-kg-core", "scripts/tg-load-kg-core",