Expose Next.js functions as MCP tools with Dendra
Description and use
In the Next.js example, API operations are exposed as MCP tools via Dendra, enabling AI to invoke them with input validation and a single source of truth.
Problem
Integrate Next.js API operations as AI tools without duplicating logic.
Outcome
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.
Workflow
- 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
Evidence
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.
- Record date: 2026-10-19T16:41:08.672Z