@nuxtjs/mcp-toolkit 0.6.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.d.mts CHANGED
@@ -35,6 +35,23 @@ interface ModuleOptions {
35
35
  * @default 'mcp'
36
36
  */
37
37
  dir?: string;
38
+ /**
39
+ * Enable MCP session management (stateful transport).
40
+ * When enabled, the server assigns session IDs and maintains state across requests,
41
+ * enabling SSE streaming, server-to-client notifications, and resumability.
42
+ *
43
+ * Pass `true` for defaults or an object to configure session behavior.
44
+ * @default false
45
+ * @see https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-management
46
+ */
47
+ sessions?: boolean | {
48
+ enabled?: boolean;
49
+ /**
50
+ * Maximum session duration in milliseconds. Sessions inactive longer than this are cleaned up.
51
+ * @default 1800000 (30 minutes)
52
+ */
53
+ maxDuration?: number;
54
+ };
38
55
  }
39
56
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
40
57
 
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxtjs/mcp-toolkit",
3
- "version": "0.6.4",
3
+ "version": "0.8.0",
4
4
  "configKey": "mcp",
5
5
  "docs": "https://mcp-toolkit.nuxt.dev/getting-started/installation",
6
6
  "mcp": "https://mcp-toolkit.nuxt.dev/mcp",
package/dist/module.mjs CHANGED
@@ -72,7 +72,7 @@ function generateDeeplinkUrl(baseUrl, route, ide, serverName) {
72
72
  }
73
73
 
74
74
  const name = "@nuxtjs/mcp-toolkit";
75
- const version = "0.6.4";
75
+ const version = "0.8.0";
76
76
 
77
77
  const log = logger.withTag("@nuxtjs/mcp-toolkit");
78
78
  const { resolve } = createResolver(import.meta.url);
@@ -86,11 +86,15 @@ const module$1 = defineNuxtModule({
86
86
  },
87
87
  defaults: defaultMcpConfig,
88
88
  async setup(options, nuxt) {
89
- if (nuxt.options.nitro.static || nuxt.options._generate) {
89
+ const nitroOptions = nuxt.options.nitro;
90
+ if (nitroOptions?.static || nuxt.options._generate) {
90
91
  log.warn("@nuxtjs/mcp-toolkit is not compatible with `nuxt generate` as it needs a server to run.");
91
92
  return;
92
93
  }
93
94
  const resolver = createResolver(import.meta.url);
95
+ if (typeof options.sessions === "boolean") {
96
+ options.sessions = { enabled: options.sessions };
97
+ }
94
98
  const mcpConfig = getMcpConfig(options);
95
99
  if (!options.enabled) {
96
100
  return;
@@ -113,7 +117,8 @@ const module$1 = defineNuxtModule({
113
117
  let mcpSummary = null;
114
118
  nuxt.hook("modules:done", async () => {
115
119
  try {
116
- await nuxt.callHook("mcp:definitions:paths", paths);
120
+ const callCustomHook = nuxt.callHook;
121
+ await callCustomHook("mcp:definitions:paths", paths);
117
122
  const result = await loadAllDefinitions(paths);
118
123
  if (result.handlers && result.handlers.count > 0) {
119
124
  addServerHandler({
@@ -162,10 +167,12 @@ const module$1 = defineNuxtModule({
162
167
  path: resolver.resolve("runtime/server/types.server.d.ts")
163
168
  });
164
169
  });
165
- nuxt.options.nitro.typescript ??= {};
166
- nuxt.options.nitro.typescript.tsConfig ??= {};
167
- nuxt.options.nitro.typescript.tsConfig.include ??= [];
168
- nuxt.options.nitro.typescript.tsConfig.include.push(resolver.resolve("runtime/server/types.server.d.ts"));
170
+ if (nitroOptions) {
171
+ nitroOptions.typescript ??= {};
172
+ nitroOptions.typescript.tsConfig ??= {};
173
+ nitroOptions.typescript.tsConfig.include ??= [];
174
+ nitroOptions.typescript.tsConfig.include.push(resolver.resolve("runtime/server/types.server.d.ts"));
175
+ }
169
176
  let isCloudflare = false;
170
177
  if (!nuxt.options.dev) {
171
178
  nuxt.hook("nitro:config", (nitroConfig) => {
@@ -1,3 +1,7 @@
1
+ export interface McpSessionsConfig {
2
+ enabled: boolean;
3
+ maxDuration: number;
4
+ }
1
5
  export interface McpConfig {
2
6
  enabled: boolean;
3
7
  route: string;
@@ -5,6 +9,7 @@ export interface McpConfig {
5
9
  name: string;
6
10
  version: string;
7
11
  dir: string;
12
+ sessions: McpSessionsConfig;
8
13
  }
9
14
  export declare const defaultMcpConfig: McpConfig;
10
15
  export declare function getMcpConfig(partial?: Partial<McpConfig>): McpConfig;
@@ -5,7 +5,12 @@ export const defaultMcpConfig = {
5
5
  browserRedirect: "/",
6
6
  name: "",
7
7
  version: "1.0.0",
8
- dir: "mcp"
8
+ dir: "mcp",
9
+ sessions: {
10
+ enabled: false,
11
+ maxDuration: 30 * 60 * 1e3
12
+ // 30 minutes
13
+ }
9
14
  };
10
15
  export function getMcpConfig(partial) {
11
16
  return defu(partial, defaultMcpConfig);
@@ -1,9 +1,27 @@
1
1
  import type { ZodRawShape } from 'zod';
2
- import type { CallToolResult, ServerRequest, ServerNotification, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
2
+ import type { CallToolResult, ServerRequest, ServerNotification } from '@modelcontextprotocol/sdk/types.js';
3
3
  import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
4
4
  import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
5
5
  import type { ShapeOutput } from '@modelcontextprotocol/sdk/server/zod-compat.js';
6
6
  import { type MsCacheDuration, type McpCacheOptions, type McpCache } from './cache.js';
7
+ /**
8
+ * Hints that describe tool behavior to MCP clients.
9
+ *
10
+ * Clients may use these to decide whether to prompt the user for confirmation (human-in-the-loop).
11
+ * All properties are optional hints — they are not guaranteed to be respected by every client.
12
+ *
13
+ * @see https://modelcontextprotocol.io/docs/concepts/tools#tool-annotations
14
+ */
15
+ export interface McpToolAnnotations {
16
+ /** If `true`, the tool does not modify any state (e.g. a read/search/lookup). Defaults to `false`. */
17
+ readOnlyHint?: boolean;
18
+ /** If `true`, the tool may perform destructive operations like deleting data. Only meaningful when `readOnlyHint` is `false`. Defaults to `true`. */
19
+ destructiveHint?: boolean;
20
+ /** If `true`, calling the tool multiple times with the same arguments has no additional effect beyond the first call. Only meaningful when `readOnlyHint` is `false`. Defaults to `false`. */
21
+ idempotentHint?: boolean;
22
+ /** If `true`, the tool may interact with the outside world (e.g. external APIs, the internet). If `false`, the tool only operates on local/internal data. Defaults to `true`. */
23
+ openWorldHint?: boolean;
24
+ }
7
25
  export type { MsCacheDuration };
8
26
  export type McpToolCacheOptions<Args = unknown> = McpCacheOptions<Args>;
9
27
  export type McpToolCache<Args = unknown> = McpCache<Args>;
@@ -21,7 +39,8 @@ export interface McpToolDefinition<InputSchema extends ZodRawShape | undefined =
21
39
  description?: string;
22
40
  inputSchema?: InputSchema;
23
41
  outputSchema?: OutputSchema;
24
- annotations?: ToolAnnotations;
42
+ annotations?: McpToolAnnotations;
43
+ inputExamples?: InputSchema extends ZodRawShape ? Partial<ShapeOutput<InputSchema>>[] : never;
25
44
  _meta?: Record<string, unknown>;
26
45
  handler: McpToolCallback<InputSchema>;
27
46
  /**
@@ -25,7 +25,10 @@ export function registerToolFromDefinition(server, tool) {
25
25
  inputSchema: tool.inputSchema,
26
26
  outputSchema: tool.outputSchema,
27
27
  annotations: tool.annotations,
28
- _meta: tool._meta
28
+ _meta: {
29
+ ...tool._meta,
30
+ ...tool.inputExamples && { inputExamples: tool.inputExamples }
31
+ }
29
32
  };
30
33
  return server.registerTool(name, options, handler);
31
34
  }
@@ -120,7 +120,14 @@ async function launchMcpInspector(nuxt, options) {
120
120
  if (inspectorProcess) {
121
121
  return;
122
122
  }
123
- const mcpServerUrl = `http://localhost:${nuxt.options.devServer?.port || 3e3}${options.route || "/mcp"}`;
123
+ const devServerUrl = nuxt.options.devServer?.url;
124
+ let mcpServerUrl;
125
+ if (devServerUrl) {
126
+ mcpServerUrl = `${devServerUrl.replace(/\/$/, "")}${options.route || "/mcp"}`;
127
+ } else {
128
+ const protocol = nuxt.options.devServer?.https ? "https" : "http";
129
+ mcpServerUrl = `${protocol}://localhost:${nuxt.options.devServer?.port || 3e3}${options.route || "/mcp"}`;
130
+ }
124
131
  const inspectorClientPort = getInspectorClientPort();
125
132
  const inspectorServerPort = getInspectorServerPort();
126
133
  const inspectorBaseUrl = buildInspectorBaseUrl(inspectorClientPort);
@@ -305,7 +312,9 @@ export function addDevToolsCustomTabs(nuxt, options) {
305
312
  if (!options.enabled) {
306
313
  return;
307
314
  }
308
- nuxt.hook("devtools:customTabs", (tabs) => {
315
+ const registerCustomHook = nuxt.hook;
316
+ registerCustomHook("devtools:customTabs", (tabsArg) => {
317
+ const tabs = tabsArg;
309
318
  tabs.push({
310
319
  category: "server",
311
320
  name: "mcp-inspector",
@@ -6,7 +6,8 @@ const fallbackCtx = {
6
6
  passThroughOnException: () => {
7
7
  }
8
8
  };
9
- export default createMcpTransportHandler(async (server, event) => {
9
+ export default createMcpTransportHandler(async (createServer, event) => {
10
+ const server = createServer();
10
11
  const { createMcpHandler } = await import("agents/mcp");
11
12
  const handler = createMcpHandler(server, {
12
13
  route: ""
@@ -1,13 +1,83 @@
1
1
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
2
- import { readBody } from "h3";
2
+ import { randomUUID } from "uncrypto";
3
+ import { readBody, getHeader, getMethod } from "h3";
4
+ import config from "#nuxt-mcp-toolkit/config.mjs";
3
5
  import { createMcpTransportHandler } from "./types.js";
4
- export default createMcpTransportHandler(async (server, event) => {
5
- const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
6
- event.node.res.on("close", () => {
7
- transport.close();
8
- server.close();
6
+ const sessions = /* @__PURE__ */ new Map();
7
+ let cleanupInterval = null;
8
+ function ensureCleanup(maxDuration) {
9
+ if (cleanupInterval) return;
10
+ cleanupInterval = setInterval(() => {
11
+ const now = Date.now();
12
+ for (const [id, session] of sessions) {
13
+ if (now - session.lastAccessed > maxDuration) {
14
+ session.transport.close();
15
+ session.server.close();
16
+ sessions.delete(id);
17
+ }
18
+ }
19
+ if (sessions.size === 0 && cleanupInterval) {
20
+ clearInterval(cleanupInterval);
21
+ cleanupInterval = null;
22
+ }
23
+ }, 6e4);
24
+ }
25
+ export default createMcpTransportHandler(async (createServer, event) => {
26
+ const sessionsConfig = config.sessions;
27
+ const sessionsEnabled = sessionsConfig?.enabled ?? false;
28
+ if (!sessionsEnabled) {
29
+ const server2 = createServer();
30
+ const transport2 = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
31
+ event.node.res.on("close", () => {
32
+ transport2.close();
33
+ server2.close();
34
+ });
35
+ await server2.connect(transport2);
36
+ const body2 = await readBody(event);
37
+ await transport2.handleRequest(event.node.req, event.node.res, body2);
38
+ return;
39
+ }
40
+ const maxDuration = sessionsConfig?.maxDuration ?? 30 * 60 * 1e3;
41
+ const method = getMethod(event);
42
+ const sessionId = getHeader(event, "mcp-session-id");
43
+ if (sessionId) {
44
+ const session = sessions.get(sessionId);
45
+ if (!session) {
46
+ event.node.res.writeHead(404, { "Content-Type": "application/json" });
47
+ event.node.res.end(JSON.stringify({
48
+ jsonrpc: "2.0",
49
+ error: { code: -32001, message: "Session not found" },
50
+ id: null
51
+ }));
52
+ return;
53
+ }
54
+ session.lastAccessed = Date.now();
55
+ const body2 = method === "POST" ? await readBody(event) : void 0;
56
+ await session.transport.handleRequest(event.node.req, event.node.res, body2);
57
+ return;
58
+ }
59
+ const server = createServer();
60
+ let sessionStored = false;
61
+ const transport = new StreamableHTTPServerTransport({
62
+ sessionIdGenerator: () => randomUUID(),
63
+ onsessioninitialized: (id) => {
64
+ sessionStored = true;
65
+ sessions.set(id, { server, transport, lastAccessed: Date.now() });
66
+ ensureCleanup(maxDuration);
67
+ }
9
68
  });
69
+ transport.onclose = () => {
70
+ const sid = transport.sessionId;
71
+ if (sid && sessions.has(sid)) {
72
+ sessions.delete(sid);
73
+ }
74
+ server.close();
75
+ };
10
76
  await server.connect(transport);
11
77
  const body = await readBody(event);
12
78
  await transport.handleRequest(event.node.req, event.node.res, body);
79
+ if (!sessionStored) {
80
+ transport.close();
81
+ server.close();
82
+ }
13
83
  });
@@ -1,4 +1,4 @@
1
1
  import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
2
  import type { H3Event } from 'h3';
3
- export type McpTransportHandler = (server: McpServer, event: H3Event) => Promise<Response | void> | Response | void;
3
+ export type McpTransportHandler = (createServer: () => McpServer, event: H3Event) => Promise<Response | void> | Response | void;
4
4
  export declare const createMcpTransportHandler: (handler: McpTransportHandler) => McpTransportHandler;
@@ -1,6 +1,9 @@
1
1
  import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
2
  import type { H3Event } from 'h3';
3
- import type { McpToolDefinition, McpResourceDefinition, McpPromptDefinition, McpMiddleware } from './definitions/index.js';
3
+ import type { McpMiddleware } from './definitions/handlers.js';
4
+ import type { McpPromptDefinition } from './definitions/prompts.js';
5
+ import type { McpResourceDefinition } from './definitions/resources.js';
6
+ import type { McpToolDefinition } from './definitions/tools.js';
4
7
  export type { McpTransportHandler } from './providers/types.js';
5
8
  export { createMcpTransportHandler } from './providers/types.js';
6
9
  export interface ResolvedMcpConfig {
@@ -1,11 +1,24 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { sendRedirect, getHeader, defineEventHandler } from "h3";
3
- import { registerToolFromDefinition, registerResourceFromDefinition, registerPromptFromDefinition } from "./definitions/index.js";
3
+ import { registerPromptFromDefinition } from "./definitions/prompts.js";
4
+ import { registerResourceFromDefinition } from "./definitions/resources.js";
5
+ import { registerToolFromDefinition } from "./definitions/tools.js";
4
6
  import handleMcpRequest from "#nuxt-mcp-toolkit/transport.mjs";
5
7
  export { createMcpTransportHandler } from "./providers/types.js";
6
8
  function resolveConfig(config, event) {
7
9
  return typeof config === "function" ? config(event) : config;
8
10
  }
11
+ function registerEmptyDefinitionFallbacks(server, config) {
12
+ if (!config.tools?.length) {
13
+ server.registerTool("__init__", {}, async () => ({ content: [] })).remove();
14
+ }
15
+ if (!config.resources?.length) {
16
+ server.registerResource("__init__", "noop://init", {}, async () => ({ contents: [] })).remove();
17
+ }
18
+ if (!config.prompts?.length) {
19
+ server.registerPrompt("__init__", {}, async () => ({ messages: [] })).remove();
20
+ }
21
+ }
9
22
  export function createMcpServer(config) {
10
23
  const server = new McpServer({
11
24
  name: config.name,
@@ -20,6 +33,7 @@ export function createMcpServer(config) {
20
33
  for (const prompt of config.prompts || []) {
21
34
  registerPromptFromDefinition(server, prompt);
22
35
  }
36
+ registerEmptyDefinitionFallbacks(server, config);
23
37
  return server;
24
38
  }
25
39
  export function createMcpHandler(config) {
@@ -29,8 +43,7 @@ export function createMcpHandler(config) {
29
43
  return sendRedirect(event, resolvedConfig.browserRedirect);
30
44
  }
31
45
  const handler = async () => {
32
- const server = createMcpServer(resolvedConfig);
33
- return handleMcpRequest(server, event);
46
+ return handleMcpRequest(() => createMcpServer(resolvedConfig), event);
34
47
  };
35
48
  if (resolvedConfig.middleware) {
36
49
  let nextCalled = false;
@@ -3,6 +3,10 @@ declare module '@nuxt/schema' {
3
3
  /**
4
4
  * Add additional directories to scan for MCP definition files (tools, resources, prompts, handlers).
5
5
  * @param paths - Object containing arrays of directory paths for each definition type.
6
+ * @param paths.tools - Array of directory paths to scan for tool definitions.
7
+ * @param paths.resources - Array of directory paths to scan for resource definitions.
8
+ * @param paths.prompts - Array of directory paths to scan for prompt definitions.
9
+ * @param paths.handlers - Array of directory paths to scan for handler definitions.
6
10
  * @returns void | Promise<void>
7
11
  */
8
12
  'mcp:definitions:paths': (paths: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxtjs/mcp-toolkit",
3
- "version": "0.6.4",
3
+ "version": "0.8.0",
4
4
  "description": "Create MCP servers directly in your Nuxt application. Define tools, resources, and prompts with a simple and intuitive API.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,20 +39,24 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@modelcontextprotocol/sdk": "^1.26.0",
43
- "@nuxt/kit": "^4.3.1",
42
+ "@modelcontextprotocol/sdk": "^1.27.1",
43
+ "@nuxt/kit": "^4.4.2",
44
44
  "defu": "^6.1.4",
45
45
  "ms": "^2.1.3",
46
46
  "pathe": "^2.0.3",
47
- "satori": "^0.19.1",
47
+ "satori": "^0.25.0",
48
48
  "scule": "^1.3.0",
49
49
  "tinyglobby": "^0.2.15"
50
50
  },
51
51
  "peerDependencies": {
52
+ "h3": "^1.15.6",
52
53
  "zod": "^4.1.13",
53
- "agents": ">=0.4.0"
54
+ "agents": ">=0.7.6"
54
55
  },
55
56
  "peerDependenciesMeta": {
57
+ "h3": {
58
+ "optional": false
59
+ },
56
60
  "zod": {
57
61
  "optional": false
58
62
  },
@@ -61,18 +65,18 @@
61
65
  }
62
66
  },
63
67
  "devDependencies": {
64
- "@nuxt/devtools": "^3.1.1",
65
- "@nuxt/eslint-config": "^1.15.1",
68
+ "@nuxt/devtools": "^3.2.3",
69
+ "@nuxt/eslint-config": "^1.15.2",
66
70
  "@nuxt/module-builder": "^1.0.2",
67
- "@nuxt/schema": "^4.3.1",
68
- "@nuxt/test-utils": "^3.23.0",
71
+ "@nuxt/schema": "^4.4.2",
72
+ "@nuxt/test-utils": "^4.0.0",
69
73
  "@types/node": "latest",
70
74
  "changelogen": "^0.6.2",
71
- "eslint": "^9.39.2",
72
- "nuxt": "^4.3.1",
75
+ "eslint": "^9.39.4",
76
+ "nuxt": "^4.4.2",
73
77
  "typescript": "~5.9.3",
74
- "vitest": "^4.0.18",
75
- "vue-tsc": "^3.2.4"
78
+ "vitest": "^4.1.0",
79
+ "vue-tsc": "^3.2.5"
76
80
  },
77
81
  "publishConfig": {
78
82
  "access": "public"