Expose Next.js functions as MCP tools with Dendra
Описание и применение
В примере Next.js API‑операции экспонируются как инструменты MCP через Dendra, позволяя 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