mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-26 08:56:21 +02:00
Updated CLI invocation and config model for tools and mcp (#438)
* Updated CLI invocation and config model for tools and mcp * CLI anomalies * Tweaked the MCP tool implementation for new model * Update agent implementation to match the new model * Fix agent tools, now all tested * Fixed integration tests * Fix MCP delete tool params
This commit is contained in:
parent
a96d02da5d
commit
81c7c1181b
11 changed files with 270 additions and 183 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
"""
|
||||
Deletes MCP (Model Control Protocol) tools from the TrustGraph system.
|
||||
Removes MCP tool configurations by name from the 'mcp' configuration group.
|
||||
Removes MCP tool configurations by ID from the 'mcp' configuration group.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
|
|
@ -14,7 +14,7 @@ default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
|||
|
||||
def delete_mcp_tool(
|
||||
url : str,
|
||||
name : str,
|
||||
id : str,
|
||||
):
|
||||
|
||||
api = Api(url).config()
|
||||
|
|
@ -22,28 +22,28 @@ def delete_mcp_tool(
|
|||
# Check if the tool exists first
|
||||
try:
|
||||
values = api.get([
|
||||
ConfigKey(type="mcp", key=name)
|
||||
ConfigKey(type="mcp", key=id)
|
||||
])
|
||||
|
||||
if not values or not values[0].value:
|
||||
print(f"MCP tool '{name}' not found.")
|
||||
print(f"MCP tool '{id}' not found.")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"MCP tool '{name}' not found.")
|
||||
print(f"MCP tool '{id}' not found.")
|
||||
return False
|
||||
|
||||
# Delete the MCP tool configuration from the 'mcp' group
|
||||
try:
|
||||
api.delete([
|
||||
ConfigKey(type="mcp", key=name)
|
||||
ConfigKey(type="mcp", key=id)
|
||||
])
|
||||
|
||||
print(f"MCP tool '{name}' deleted successfully.")
|
||||
print(f"MCP tool '{id}' deleted successfully.")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error deleting MCP tool '{name}': {e}")
|
||||
print(f"Error deleting MCP tool '{id}': {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
|
|
@ -56,9 +56,9 @@ def main():
|
|||
Once deleted, the tool will no longer be available for use.
|
||||
|
||||
Examples:
|
||||
%(prog)s --name weather
|
||||
%(prog)s --name calculator
|
||||
%(prog)s --api-url http://localhost:9000/ --name file-reader
|
||||
%(prog)s --id weather
|
||||
%(prog)s --id calculator
|
||||
%(prog)s --api-url http://localhost:9000/ --id file-reader
|
||||
''').strip(),
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter
|
||||
)
|
||||
|
|
@ -70,21 +70,21 @@ def main():
|
|||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
'--id',
|
||||
required=True,
|
||||
help='MCP tool name to delete',
|
||||
help='MCP tool ID to delete',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
if not args.name:
|
||||
raise RuntimeError("Must specify --name for MCP tool to delete")
|
||||
if not args.id:
|
||||
raise RuntimeError("Must specify --id for MCP tool to delete")
|
||||
|
||||
delete_mcp_tool(
|
||||
url=args.api_url,
|
||||
name=args.name
|
||||
id=args.id
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -21,27 +21,10 @@ def delete_tool(
|
|||
|
||||
api = Api(url).config()
|
||||
|
||||
# Get the current tool index
|
||||
try:
|
||||
values = api.get([
|
||||
ConfigKey(type="agent", key="tool-index")
|
||||
])
|
||||
|
||||
ix = json.loads(values[0].value)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error reading tool index: {e}")
|
||||
return False
|
||||
|
||||
# Check if the tool exists in the index
|
||||
if id not in ix:
|
||||
print(f"Tool '{id}' not found in tool index.")
|
||||
return False
|
||||
|
||||
# Check if the tool configuration exists
|
||||
try:
|
||||
tool_values = api.get([
|
||||
ConfigKey(type="agent", key=f"tool.{id}")
|
||||
ConfigKey(type="tool", key=id)
|
||||
])
|
||||
|
||||
if not tool_values or not tool_values[0].value:
|
||||
|
|
@ -52,22 +35,12 @@ def delete_tool(
|
|||
print(f"Tool configuration for '{id}' not found.")
|
||||
return False
|
||||
|
||||
# Remove the tool ID from the index
|
||||
ix.remove(id)
|
||||
|
||||
# Delete the tool configuration and update the index
|
||||
try:
|
||||
|
||||
# Update the tool index
|
||||
api.put([
|
||||
ConfigValue(
|
||||
type="agent", key="tool-index", value=json.dumps(ix)
|
||||
)
|
||||
])
|
||||
|
||||
# Delete the tool configuration
|
||||
api.delete([
|
||||
ConfigKey(type="agent", key=f"tool.{id}")
|
||||
ConfigKey(type="tool", key=id)
|
||||
])
|
||||
|
||||
print(f"Tool '{id}' deleted successfully.")
|
||||
|
|
|
|||
54
trustgraph-cli/scripts/tg-set-mcp-tool
Normal file → Executable file
54
trustgraph-cli/scripts/tg-set-mcp-tool
Normal file → Executable file
|
|
@ -1,10 +1,17 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Configures and registers MCP (Model Control Protocol) tools in the
|
||||
TrustGraph system. Allows defining MCP tool configurations with name and
|
||||
URL. Tools are stored in the 'mcp' configuration group for discovery and
|
||||
execution.
|
||||
Configures and registers MCP (Model Context Protocol) tools in the
|
||||
TrustGraph system.
|
||||
|
||||
MCP tools are external services that follow the Model Context Protocol
|
||||
specification. This script stores MCP tool configurations with:
|
||||
- id: Unique identifier for the tool
|
||||
- remote-name: Name used by the MCP server (defaults to id)
|
||||
- url: MCP server endpoint URL
|
||||
|
||||
Configurations are stored in the 'mcp' configuration group and can be
|
||||
referenced by agent tools using the 'mcp-tool' type.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
|
|
@ -17,7 +24,8 @@ default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
|||
|
||||
def set_mcp_tool(
|
||||
url : str,
|
||||
name : str,
|
||||
id : str,
|
||||
remote_name : str,
|
||||
tool_url : str,
|
||||
):
|
||||
|
||||
|
|
@ -26,15 +34,13 @@ def set_mcp_tool(
|
|||
# Store the MCP tool configuration in the 'mcp' group
|
||||
values = api.put([
|
||||
ConfigValue(
|
||||
type="mcp", key=name, value=json.dumps({
|
||||
"name": name,
|
||||
type="mcp", key=id, value=json.dumps({
|
||||
"remote-name": remote_name,
|
||||
"url": tool_url,
|
||||
})
|
||||
)
|
||||
])
|
||||
|
||||
print(f"MCP tool '{name}' set with URL: {tool_url}")
|
||||
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
|
|
@ -45,8 +51,8 @@ def main():
|
|||
to the MCP server endpoint that provides the tool functionality.
|
||||
|
||||
Examples:
|
||||
%(prog)s --name weather --tool-url "http://localhost:3000/weather"
|
||||
%(prog)s --name calculator --tool-url "http://mcp-tools.example.com/calc"
|
||||
%(prog)s --id weather --tool-url "http://localhost:3000/weather"
|
||||
%(prog)s --id calculator --tool-url "http://mcp-tools.example.com/calc"
|
||||
''').strip(),
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter
|
||||
)
|
||||
|
|
@ -58,9 +64,15 @@ def main():
|
|||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
'-i', '--id',
|
||||
required=True,
|
||||
help='MCP tool name',
|
||||
help='MCP tool identifier',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-r', '--remote-name',
|
||||
required=False,
|
||||
help='Remote MCP tool name (defaults to --id if not specified)',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
|
|
@ -73,15 +85,21 @@ def main():
|
|||
|
||||
try:
|
||||
|
||||
if not args.name:
|
||||
raise RuntimeError("Must specify --name for MCP tool")
|
||||
if not args.id:
|
||||
raise RuntimeError("Must specify --id for MCP tool")
|
||||
|
||||
if not args.tool_url:
|
||||
raise RuntimeError("Must specify --url for MCP tool")
|
||||
raise RuntimeError("Must specify --tool-url for MCP tool")
|
||||
|
||||
if args.remote_name:
|
||||
remote_name = args.remote_name
|
||||
else:
|
||||
remote_name = args.id
|
||||
|
||||
set_mcp_tool(
|
||||
url=args.api_url,
|
||||
name=args.name,
|
||||
url=args.api_url,
|
||||
id=args.id,
|
||||
remote_name=remote_name,
|
||||
tool_url=args.tool_url
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,15 @@
|
|||
|
||||
"""
|
||||
Configures and registers tools in the TrustGraph system.
|
||||
Allows defining tool metadata including ID, name, description, type,
|
||||
and argument specifications. Tools are stored in the agent configuration
|
||||
and indexed for discovery and execution.
|
||||
|
||||
This script allows you to define agent tools with various types including:
|
||||
- knowledge-query: Query knowledge bases
|
||||
- text-completion: Text generation
|
||||
- mcp-tool: Reference to MCP (Model Context Protocol) tools
|
||||
- prompt: Prompt template execution
|
||||
|
||||
Tools are stored in the 'tool' configuration group and can include
|
||||
argument specifications for parameterized execution.
|
||||
"""
|
||||
|
||||
from typing import List
|
||||
|
|
@ -51,6 +57,9 @@ def set_tool(
|
|||
name : str,
|
||||
description : str,
|
||||
type : str,
|
||||
mcp_tool : str,
|
||||
collection : str,
|
||||
template : str,
|
||||
arguments : List[Argument],
|
||||
):
|
||||
|
||||
|
|
@ -60,14 +69,20 @@ def set_tool(
|
|||
ConfigKey(type="agent", key="tool-index")
|
||||
])
|
||||
|
||||
ix = json.loads(values[0].value)
|
||||
|
||||
object = {
|
||||
"id": id,
|
||||
"name": name,
|
||||
"description": description,
|
||||
"type": type,
|
||||
"arguments": [
|
||||
}
|
||||
|
||||
if mcp_tool: object["mcp-tool"] = mcp_tool
|
||||
|
||||
if collection: object["collection"] = collection
|
||||
|
||||
if template: object["template"] = template
|
||||
|
||||
if arguments:
|
||||
object["arguments"] = [
|
||||
{
|
||||
"name": a.name,
|
||||
"type": a.type,
|
||||
|
|
@ -75,17 +90,10 @@ def set_tool(
|
|||
}
|
||||
for a in arguments
|
||||
]
|
||||
}
|
||||
|
||||
if id not in ix:
|
||||
ix.append(id)
|
||||
|
||||
values = api.put([
|
||||
ConfigValue(
|
||||
type="agent", key="tool-index", value=json.dumps(ix)
|
||||
),
|
||||
ConfigValue(
|
||||
type="agent", key=f"tool.{id}", value=json.dumps(object)
|
||||
type="tool", key=f"{id}", value=json.dumps(object)
|
||||
)
|
||||
])
|
||||
|
||||
|
|
@ -100,7 +108,8 @@ def main():
|
|||
Valid tool types:
|
||||
knowledge-query - Query knowledge bases
|
||||
text-completion - Text completion/generation
|
||||
mcp-tool - Model Control Protocol tool
|
||||
mcp-tool - Model Control Protocol tool
|
||||
prompt - Prompt template query
|
||||
|
||||
Valid argument types:
|
||||
string - String/text parameter
|
||||
|
|
@ -128,28 +137,43 @@ def main():
|
|||
|
||||
parser.add_argument(
|
||||
'--id',
|
||||
help=f'Tool ID',
|
||||
help=f'Unique tool identifier',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
help=f'Tool name',
|
||||
help=f'Human-readable tool name',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--description',
|
||||
help=f'Tool description',
|
||||
help=f'Detailed description of what the tool does',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--type',
|
||||
help=f'Tool type, one of: knowledge-query, text-completion, mcp-tool',
|
||||
help=f'Tool type, one of: knowledge-query, text-completion, mcp-tool, prompt',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--argument',
|
||||
nargs="*",
|
||||
help=f'Arguments, form: name:type:description',
|
||||
'--mcp-tool',
|
||||
help=f'For MCP type: ID of MCP tool configuration (as defined by tg-set-mcp-tool)',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--collection',
|
||||
help=f'For knowledge-query type: collection to query',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--template',
|
||||
help=f'For prompt type: template ID to use',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--argument',
|
||||
nargs="*",
|
||||
help=f'Tool arguments in the form: name:type:description (can specify multiple)',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
|
@ -157,14 +181,14 @@ def main():
|
|||
try:
|
||||
|
||||
valid_types = [
|
||||
"knowledge-query", "text-completion", "mcp-tool"
|
||||
"knowledge-query", "text-completion", "mcp-tool", "prompt"
|
||||
]
|
||||
|
||||
if args.id is None:
|
||||
raise RuntimeError("Must specify --id for prompt")
|
||||
raise RuntimeError("Must specify --id for tool")
|
||||
|
||||
if args.name is None:
|
||||
raise RuntimeError("Must specify --name for prompt")
|
||||
raise RuntimeError("Must specify --name for tool")
|
||||
|
||||
if args.type:
|
||||
if args.type not in valid_types:
|
||||
|
|
@ -172,6 +196,8 @@ def main():
|
|||
"Type must be one of: " + ", ".join(valid_types)
|
||||
)
|
||||
|
||||
mcp_tool = args.mcp_tool
|
||||
|
||||
if args.argument:
|
||||
arguments = [
|
||||
Argument.parse(a)
|
||||
|
|
@ -181,10 +207,15 @@ def main():
|
|||
arguments = []
|
||||
|
||||
set_tool(
|
||||
url=args.api_url, id=args.id, name=args.name,
|
||||
url=args.api_url,
|
||||
id=args.id,
|
||||
name=args.name,
|
||||
description=args.description,
|
||||
type=args.type,
|
||||
arguments=arguments
|
||||
mcp_tool=mcp_tool,
|
||||
collection=args.collection,
|
||||
template=args.template,
|
||||
arguments=arguments,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Dumps out the current agent tool configuration
|
||||
Displays the current MCP (Model Context Protocol) tool configuration
|
||||
"""
|
||||
|
||||
import argparse
|
||||
|
|
@ -26,11 +26,10 @@ def show_config(url):
|
|||
table = []
|
||||
|
||||
table.append(("id", value.key))
|
||||
table.append(("name", data["name"]))
|
||||
table.append(("remote-name", data["remote-name"]))
|
||||
table.append(("url", data["url"]))
|
||||
|
||||
print()
|
||||
print(value.key + ":")
|
||||
|
||||
print(tabulate.tabulate(
|
||||
table,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Dumps out the current agent tool configuration
|
||||
Displays the current agent tool configurations
|
||||
|
||||
Shows all configured tools including their types:
|
||||
- knowledge-query: Tools that query knowledge bases
|
||||
- text-completion: Tools for text generation
|
||||
- mcp-tool: References to MCP (Model Context Protocol) tools
|
||||
- prompt: Tools that execute prompt templates
|
||||
"""
|
||||
|
||||
import argparse
|
||||
|
|
@ -17,37 +23,37 @@ def show_config(url):
|
|||
|
||||
api = Api(url).config()
|
||||
|
||||
values = api.get([
|
||||
ConfigKey(type="agent", key="tool-index")
|
||||
])
|
||||
values = api.get_values(type="tool")
|
||||
|
||||
ix = json.loads(values[0].value)
|
||||
for item in values:
|
||||
|
||||
values = api.get([
|
||||
ConfigKey(type="agent", key=f"tool.{v}")
|
||||
for v in ix
|
||||
])
|
||||
id = item.key
|
||||
data = json.loads(item.value)
|
||||
|
||||
for n, key in enumerate(ix):
|
||||
|
||||
data = json.loads(values[n].value)
|
||||
tp = data["type"]
|
||||
|
||||
table = []
|
||||
|
||||
table.append(("id", data["id"]))
|
||||
table.append(("id", id))
|
||||
table.append(("name", data["name"]))
|
||||
table.append(("description", data["description"]))
|
||||
table.append(("type", data["type"]))
|
||||
table.append(("type", tp))
|
||||
|
||||
for n, arg in enumerate(data["arguments"]):
|
||||
table.append((
|
||||
f"arg {n}",
|
||||
f"{arg['name']}: {arg['type']}\n{arg['description']}"
|
||||
))
|
||||
|
||||
if tp == "mcp-tool":
|
||||
table.append(("mcp-tool", data["mcp-tool"]))
|
||||
|
||||
if tp == "knowledge-query":
|
||||
table.append(("collection", data["collection"]))
|
||||
|
||||
if tp == "prompt":
|
||||
table.append(("template", data["template"]))
|
||||
for n, arg in enumerate(data["arguments"]):
|
||||
table.append((
|
||||
f"arg {n}",
|
||||
f"{arg['name']}: {arg['type']}\n{arg['description']}"
|
||||
))
|
||||
|
||||
print()
|
||||
print(key + ":")
|
||||
|
||||
print(tabulate.tabulate(
|
||||
table,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue