Expose Next.js functions as MCP tools with Dendra
说明与用途
在 Next.js 示例中,通过 Dendra 将 API 操作暴露为 MCP 工具,使 AI 能够使用输入验证和单一逻辑源来调用它们。
问题
Integrate Next.js API operations as AI tools without duplicating logic.
结果
The MCP endpoint and application API share the same data store; tools defined with .ai() are callable via MCP with input validation, and errors are returned in JSON.
工作流
- Clone Dendra Next.js example repository
- Install dependencies and run dev server
- Open demo page to verify server
- Define operation in server/router.ts with .ai() to expose as tool
- Ensure router mounts in app/rpc and app/api/mcp
- Initialize MCP connection via curl
- List available tools to confirm notes_create and notes_list
- Call notes_create via MCP to create a note
- Retrieve notes via RPC to verify persistence
- Test validation by calling notes_create with invalid title
- Observe error response
- Use MCP tool in application context
证据
Expose Next.js operations as MCP tools with Dendra, using the same input validation and application logic as your API.
The same handler serves the application API and tool execution. There is no second copy of the note creation logic to maintain.
The example mounts the router in these files: server/router.ts, app/rpc/[...path]/route.ts, app/api/mcp/route.ts, server/context.ts.
Create a note through MCP: curl ... method":"tools/call","params":{"name":"notes_create","arguments":{"title":"One procedure, two interfaces","body":"Created through Dendra MCP."}}
Repeat the notes_create call with a numeric title: ... "title":123 ... The tool result should contain isError: true.
- 记录日期: 2026-10-19T16:41:08.672Z