@记忆法/策略
MCP(模型上下文协议)服务器,用于通过Chrome调试协议进行Mnemonica运行时分析。
概述
Strategy通过Chrome调试协议连接到正在运行的Node.js应用程序,以提取和分析Mnemonica类型层次结构。它将运行时类型与Tactica生成的类型进行比较,以验证和改进静态分析。
安装
npm install @mnemonica/strategy用法
先决条件
您的目标应用程序必须使用调试标志运行:
# For NestJS
nest start --debug --watch
# For regular Node.js
node --inspect=9229 your-app.js作为MCP服务器
npx @mnemonica/strategy使用Roo代码进行配置
添加 .roo/mcp.json:
{
"mcpServers": {
"mnemonica-strategy": {
"command": "node",
"args": ["/code/mnemonica/strategy/lib/cli.js"]
}
}
}提供的MCP工具
Strategy MCP服务器公开 3个捆绑工具:
1. execute
执行3个上下文文件夹(MCP、RPC、RUN)中的任何命令。
输入:
context(字符串,必填):执行上下文-“MCP”、“RPC”或“RUN”command(string,必填):要执行的命令名称message(string,可选):包含命令参数的JSON字符串
例子:
// Connect to NestJS debugger
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"connect\", \"host\": \"localhost\", \"port\": 9229 }"
}
// Check connection status
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"status\" }"
}
// Get runtime types
execute {
context: "RPC",
command: "get_runtime_types",
message: "{}"
}2. list
按上下文列出可用命令。
输入:
context(字符串,必填):“MCP”、“RPC”、“RUN”或“ALL”
例子:
list { context: "ALL" }3. help
获取任何命令的详细帮助。
输入:
context(字符串,必填):命令上下文command(字符串,必填):命令名称
例子:
help { context: "RPC", command: "connection" }Args传递机制(重要)
由于MCP协议的限制,命令参数必须作为 JSON字符串 在 message 字段,而不是直接对象属性。
正确格式:
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"connect\", \"host\": \"localhost\", \"port\": 9229 }"
}格式不正确(不起作用):
// DON'T DO THIS
execute {
context: "RPC",
command: "connection",
args: { action: "connect" } // This won't work!
}常用命令
连接管理
// Connect to Node.js debugger
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"connect\", \"host\": \"localhost\", \"port\": 9229 }"
}
// Check connection status
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"status\" }"
}
// Disconnect from runtime
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"disconnect\" }"
}类型分析
// Get runtime types from connected application
execute {
context: "RPC",
command: "get_runtime_types",
message: "{}"
}
// Analyze type hierarchy via CDP (retrieves complete type tree from NestJS)
execute {
context: "MCP",
command: "cdp_analyze_type_hierarchy",
message: "{}"
}
// Create type in NestJS via CDP
execute {
context: "MCP",
command: "cdp_create_type",
message: "{ \"typeName\": \"MyType\" }"
}
// Load Tactica-generated types
execute {
context: "MCP",
command: "load_remote_tactica_types",
message: "{ \"projectPath\": \"/path/to/project\" }"
}
// Compare runtime vs Tactica types
execute {
context: "MCP",
command: "compare_with_tactica",
message: "{ \"projectPath\": \"/path/to/project\" }"
}内存管理
// Store memory in connected runtime
execute {
context: "RPC",
command: "store_memory",
message: "{ \"key\": \"myKey\", \"data\": { ... } }"
}
// Recall memories
execute {
context: "RPC",
command: "recall_memories",
message: "{ \"key\": \"myKey\" }"
}工作流示例
- 使用调试模式启动应用程序:
cd tactica-examples/nestjs
npm run start:debug- 连接到调试器:
execute {
context: "RPC",
command: "connection",
message: "{ \"action\": \"connect\" }"
}- 分析运行时类型:
execute {
context: "RPC",
command: "get_runtime_types",
message: "{}"
}- 与Tactica生成的类型进行比较:
execute {
context: "MCP",
command: "compare_with_tactica",
message: "{ \"projectPath\": \"/path/to/project\" }"
}命令上下文
| 上下文 | 文件夹 | 执行环境 |
|---|---|---|
| MCP | commands-mcp/ | 本地MCP服务器进程 |
| RPC | commands-rpc/ | 通过目标Node.js中的CDP进行远程 |
| 奔跑 | commands-run/ | VS代码中的HTTP端点 |
发展
# Install dependencies
npm install
# Build
npm run build
# Watch mode
npm run watch
# Test
npm run testCDP脚本体系结构
这 cdp-scripts/ 文件夹包含通过Chrome调试协议在目标Node.js运行时内执行的脚本:
cdp-scripts/
├── create-type.js # Creates mnemonica types in NestJS
└── analyze-hierarchy.js # Retrieves complete type hierarchy它是如何工作的:
- MCP命令读取脚本文件
- 注射
var args = {...}顶部带有命令参数 - 通过发送至NestJS
client.Runtime.evaluate({ expression: script }) - 脚本在NestJS中的隔离VM上下文中执行
- Console.log输出出现在NestJS终端中(不是MCP输出)
- 返回值被发送回MCP
CDP脚本的关键模式:
// Use process.mainModule.require (not require) because CDP runs in isolated VM
var mnemonica = process.mainModule.require('mnemonica');
// Access types via subtypes Map (avoids proxy enumeration issues)
defaultCollection.subtypes.forEach(function (Type, name) {
// Process each type
});
// Recursive traversal for subtype hierarchy
function getSubtypes (Type) {
var subtypes = [];
Type.subtypes.forEach(function (SubType, name) {
subtypes.push({
name: name,
subtypes: getSubtypes(SubType) // Recursive
});
});
return subtypes;
}创建命令
命令是JavaScript文件 commands-*/ 包含MCP工具元数据的文件夹:
/**
* MCP Tool Metadata:
* {
* "name": "my_command",
* "description": "What this command does",
* "inputSchema": {
* "type": "object",
* "properties": {
* "argName": { "type": "string" }
* }
* }
* }
*/
var { require, args, store } = ctx;
// Parse message if present
var commandArgs = args;
if (args.message && typeof args.message === 'string') {
try {
commandArgs = JSON.parse(args.message);
} catch (e) {
return { success: false, error: 'Invalid JSON: ' + e.message };
}
}
// Access parsed arguments
var myArg = commandArgs.argName;
// Return result
return { success: true, data: { ... } };许可证
麻省理工学院
