MCPcopy Create free account
hub / github.com/IBM/mcp-cli / handle_memory_tool

Function handle_memory_tool

src/mcp_cli/memory/tools.py:103–143  ·  view source on GitHub ↗

Execute a memory tool and return the result as a string.

(
    store: MemoryScopeStore, tool_name: str, arguments: dict
)

Source from the content-addressed store, hash-verified

101
102
103async def handle_memory_tool(
104 store: MemoryScopeStore, tool_name: str, arguments: dict
105) -> str:
106 """Execute a memory tool and return the result as a string."""
107 try:
108 if tool_name == "remember":
109 scope = MemoryScope(arguments["scope"])
110 entry = store.remember(scope, arguments["key"], arguments["content"])
111 return f"Remembered '{entry.key}' in {scope.value} scope."
112
113 if tool_name == "recall":
114 scope_str = arguments.get("scope")
115 recall_scope: MemoryScope | None = (
116 MemoryScope(scope_str) if scope_str else None
117 )
118 key = arguments.get("key")
119 query = arguments.get("query")
120
121 entries = store.recall(scope=recall_scope, key=key, query=query)
122 if not entries:
123 return "No memories found."
124
125 lines = []
126 for e in entries:
127 lines.append(f"- [{e.key}]: {e.content}")
128 return "\n".join(lines)
129
130 if tool_name == "forget":
131 scope = MemoryScope(arguments["scope"])
132 removed = store.forget(scope, arguments["key"])
133 if removed:
134 return f"Forgot '{arguments['key']}' from {scope.value} scope."
135 return (
136 f"No memory with key '{arguments['key']}' found in {scope.value} scope."
137 )
138
139 return f"Unknown memory tool: {tool_name}"
140
141 except Exception as exc:
142 logger.warning("Memory tool %s failed: %s", tool_name, exc)
143 return f"Memory tool error: {exc}"

Callers 10

_handle_memory_toolMethod · 0.90
test_rememberMethod · 0.90
test_recall_allMethod · 0.90
test_recall_emptyMethod · 0.90
test_recall_by_keyMethod · 0.90
test_recall_by_queryMethod · 0.90
test_forget_existingMethod · 0.90
test_unknown_toolMethod · 0.90
test_error_handlingMethod · 0.90

Calls 5

MemoryScopeClass · 0.90
rememberMethod · 0.80
recallMethod · 0.80
forgetMethod · 0.80
getMethod · 0.45

Tested by 9

test_rememberMethod · 0.72
test_recall_allMethod · 0.72
test_recall_emptyMethod · 0.72
test_recall_by_keyMethod · 0.72
test_recall_by_queryMethod · 0.72
test_forget_existingMethod · 0.72
test_unknown_toolMethod · 0.72
test_error_handlingMethod · 0.72