Updated CLI invocation and config model for tools and mcp

This commit is contained in:
Cyber MacGeddon 2025-07-16 19:47:11 +01:00
parent a96d02da5d
commit 6c2a1824f5
5 changed files with 88 additions and 80 deletions

View file

@ -21,27 +21,10 @@ def delete_tool(
api = Api(url).config() 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 # Check if the tool configuration exists
try: try:
tool_values = api.get([ 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: 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.") print(f"Tool configuration for '{id}' not found.")
return False return False
# Remove the tool ID from the index
ix.remove(id)
# Delete the tool configuration and update the index # Delete the tool configuration and update the index
try: try:
# Update the tool index
api.put([
ConfigValue(
type="agent", key="tool-index", value=json.dumps(ix)
)
])
# Delete the tool configuration # Delete the tool configuration
api.delete([ api.delete([
ConfigKey(type="agent", key=f"tool.{id}") ConfigKey(type="agent", key=id)
]) ])
print(f"Tool '{id}' deleted successfully.") print(f"Tool '{id}' deleted successfully.")

View file

@ -17,7 +17,8 @@ default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
def set_mcp_tool( def set_mcp_tool(
url : str, url : str,
name : str, id : str,
remote_name : str,
tool_url : str, tool_url : str,
): ):
@ -26,15 +27,13 @@ def set_mcp_tool(
# Store the MCP tool configuration in the 'mcp' group # Store the MCP tool configuration in the 'mcp' group
values = api.put([ values = api.put([
ConfigValue( ConfigValue(
type="mcp", key=name, value=json.dumps({ type="mcp", key=id, value=json.dumps({
"name": name, "remote-name": remote_name,
"url": tool_url, "url": tool_url,
}) })
) )
]) ])
print(f"MCP tool '{name}' set with URL: {tool_url}")
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -58,11 +57,17 @@ def main():
) )
parser.add_argument( parser.add_argument(
'--name', '-i', '--id',
required=True, required=True,
help='MCP tool name', help='MCP tool name',
) )
parser.add_argument(
'-r', '--remote-name',
required=False,
help='MCP tool name',
)
parser.add_argument( parser.add_argument(
'--tool-url', '--tool-url',
required=True, required=True,
@ -73,15 +78,21 @@ def main():
try: try:
if not args.name: if not args.id:
raise RuntimeError("Must specify --name for MCP tool") raise RuntimeError("Must specify --name for MCP tool")
if not args.tool_url: if not args.tool_url:
raise RuntimeError("Must specify --url for MCP tool") raise RuntimeError("Must specify --url for MCP tool")
if args.remote_name:
remote_name = args.remote_name
else:
remote_name = args.id
set_mcp_tool( set_mcp_tool(
url=args.api_url, url=args.api_url,
name=args.name, id=args.id,
remote_name=remote_name,
tool_url=args.tool_url tool_url=args.tool_url
) )

View file

@ -51,6 +51,9 @@ def set_tool(
name : str, name : str,
description : str, description : str,
type : str, type : str,
mcp_tool : str,
collection : str,
template : str,
arguments : List[Argument], arguments : List[Argument],
): ):
@ -60,14 +63,20 @@ def set_tool(
ConfigKey(type="agent", key="tool-index") ConfigKey(type="agent", key="tool-index")
]) ])
ix = json.loads(values[0].value)
object = { object = {
"id": id,
"name": name, "name": name,
"description": description, "description": description,
"type": type, "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, "name": a.name,
"type": a.type, "type": a.type,
@ -75,17 +84,10 @@ def set_tool(
} }
for a in arguments for a in arguments
] ]
}
if id not in ix:
ix.append(id)
values = api.put([ values = api.put([
ConfigValue( ConfigValue(
type="agent", key="tool-index", value=json.dumps(ix) type="tool", key=f"{id}", value=json.dumps(object)
),
ConfigValue(
type="agent", key=f"tool.{id}", value=json.dumps(object)
) )
]) ])
@ -100,7 +102,8 @@ def main():
Valid tool types: Valid tool types:
knowledge-query - Query knowledge bases knowledge-query - Query knowledge bases
text-completion - Text completion/generation text-completion - Text completion/generation
mcp-tool - Model Control Protocol tool mcp-tool - Model Control Protocol tool
prompt - Prompt template query
Valid argument types: Valid argument types:
string - String/text parameter string - String/text parameter
@ -143,13 +146,28 @@ def main():
parser.add_argument( parser.add_argument(
'--type', '--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( parser.add_argument(
'--argument', '--mcp-tool',
nargs="*", help=f'For MCP, ID of MCP tool, as defined in TrustGraph config',
help=f'Arguments, form: name:type:description', )
parser.add_argument(
'--collection',
help=f'For knowledge query, collection to use',
)
parser.add_argument(
'--template',
help=f'For prompt, template to use',
)
parser.add_argument(
'--argument',
nargs="*",
help=f'For prompts, arguments in the form: name:type:description',
) )
args = parser.parse_args() args = parser.parse_args()
@ -157,7 +175,7 @@ def main():
try: try:
valid_types = [ valid_types = [
"knowledge-query", "text-completion", "mcp-tool" "knowledge-query", "text-completion", "mcp-tool", "prompt"
] ]
if args.id is None: if args.id is None:
@ -172,6 +190,8 @@ def main():
"Type must be one of: " + ", ".join(valid_types) "Type must be one of: " + ", ".join(valid_types)
) )
mcp_tool = args.mcp_tool
if args.argument: if args.argument:
arguments = [ arguments = [
Argument.parse(a) Argument.parse(a)
@ -181,10 +201,15 @@ def main():
arguments = [] arguments = []
set_tool( 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, description=args.description,
type=args.type, type=args.type,
arguments=arguments mcp_tool=mcp_tool,
collection=args.collection,
template=args.template,
arguments=arguments,
) )
except Exception as e: except Exception as e:

View file

@ -26,11 +26,10 @@ def show_config(url):
table = [] table = []
table.append(("id", value.key)) table.append(("id", value.key))
table.append(("name", data["name"])) table.append(("remote-name", data["remote-name"]))
table.append(("url", data["url"])) table.append(("url", data["url"]))
print() print()
print(value.key + ":")
print(tabulate.tabulate( print(tabulate.tabulate(
table, table,

View file

@ -17,37 +17,37 @@ def show_config(url):
api = Api(url).config() api = Api(url).config()
values = api.get([ values = api.get_values(type="tool")
ConfigKey(type="agent", key="tool-index")
])
ix = json.loads(values[0].value) for item in values:
values = api.get([ id = item.key
ConfigKey(type="agent", key=f"tool.{v}") data = json.loads(item.value)
for v in ix
])
for n, key in enumerate(ix): tp = data["type"]
data = json.loads(values[n].value)
table = [] table = []
table.append(("id", data["id"])) table.append(("id", id))
table.append(("name", data["name"])) table.append(("name", data["name"]))
table.append(("description", data["description"])) table.append(("description", data["description"]))
table.append(("type", data["type"])) table.append(("type", tp))
for n, arg in enumerate(data["arguments"]): if tp == "mcp-tool":
table.append(( table.append(("mcp-tool", data["mcp-tool"]))
f"arg {n}",
f"{arg['name']}: {arg['type']}\n{arg['description']}" 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()
print(key + ":")
print(tabulate.tabulate( print(tabulate.tabulate(
table, table,