executeCommand
Execute specific commands on the SiYuan Note MCP Server to manage notebooks, manipulate documents, and control content within the note system, enabling efficient data operations.
Instructions
执行指定的命令
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | No | 命令参数 | |
| type | Yes | 命令类型 |
Implementation Reference
- src/tools/commands.ts:14-39 (handler)The handler function for the 'executeCommand' MCP tool. It delegates execution to the registry's executeCommand method and formats the response as MCP format.
async ({ type, params = {} }) => { try { const result = await registry.executeCommand(type, params); return { content: [ { type: 'text' as const, text: result.content[0].text } ], _meta: { result: result._meta || {} }, isError: result.isError }; } catch (error) { return { content: [ { type: 'text' as const, text: error instanceof Error ? error.message : '命令执行失败' } ], isError: true }; } - src/tools/commands.ts:10-13 (schema)Zod input schema for the executeCommand tool: 'type' (required string, command type), 'params' (optional record of any).
{ type: z.string().describe('命令类型'), params: z.record(z.any()).optional().describe('命令参数') }, - src/tools/commands.ts:7-41 (registration)Registration of the 'executeCommand' tool on the MCP server using server.tool(), including name, description, schema, and handler.
server.tool( 'executeCommand', '执行指定的命令', { type: z.string().describe('命令类型'), params: z.record(z.any()).optional().describe('命令参数') }, async ({ type, params = {} }) => { try { const result = await registry.executeCommand(type, params); return { content: [ { type: 'text' as const, text: result.content[0].text } ], _meta: { result: result._meta || {} }, isError: result.isError }; } catch (error) { return { content: [ { type: 'text' as const, text: error instanceof Error ? error.message : '命令执行失败' } ], isError: true }; } } ); - src/utils/registry.ts:305-358 (helper)Core dispatch logic in CommandRegistry.executeCommand: resolves command name, parses/validates params, invokes handler, handles errors - called by the tool handler.
public async executeCommand(commandName: string, params: unknown = {}): Promise<McpResponse> { // 尝试直接查找完整命令名 let command = this.commands.get(commandName); if (!command) { // 如果找不到,尝试解析命名空间 const [namespace, name] = this.parseFullCommandName(commandName); const fullName = this.getFullCommandName(namespace, name); command = this.commands.get(fullName); } if (!command) { return { content: [ { type: 'text', text: `命令 ${commandName} 不存在` } ], isError: true }; } try { const validatedParams = command.params.parse(params); return await command.handler(validatedParams); } catch (error) { if (error instanceof z.ZodError) { const issues = error.issues.map(issue => ` - ${issue.path.join('.')}: ${issue.message}` ).join('\n'); return { content: [ { type: 'text', text: `参数验证失败:\n${issues}` } ], isError: true }; } return { content: [ { type: 'text', text: `命令执行失败:${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } - src/server.ts:52-52 (registration)Invocation of registerCommandTool which registers the executeCommand tool on the server instance.
registerCommandTool(server);