@lovable.dev/mcp-js 0.22.1-rc.1 → 0.22.1-rc.2

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.
Files changed (87) hide show
  1. package/dist/authorize-BMeXd_Sh.d.ts +25 -0
  2. package/dist/base-CM-KCpp6.d.ts +28 -0
  3. package/dist/chunk-5XMMDTB2.js +58 -0
  4. package/dist/chunk-6DXGZZA4.js +6 -0
  5. package/dist/chunk-6QFZYYUV.js +11 -0
  6. package/dist/chunk-B62AWJGO.js +141 -0
  7. package/dist/chunk-EAPABSON.js +6 -0
  8. package/dist/chunk-H37EB22A.js +125 -0
  9. package/dist/chunk-JRXYPPVK.js +114 -0
  10. package/dist/chunk-MA5H6PSF.js +46 -0
  11. package/dist/chunk-SYRYQM7M.js +79 -0
  12. package/dist/chunk-UQK5UO6C.js +26 -0
  13. package/dist/chunk-XGHYQJJG.js +741 -0
  14. package/dist/chunk-XQWJN6DC.js +14 -0
  15. package/dist/chunk-Y3ZFPEQH.js +8 -0
  16. package/dist/cli/extract-manifest.cjs +201 -116
  17. package/dist/cli/extract-manifest.d.cts +1 -1
  18. package/dist/cli/extract-manifest.d.ts +1 -1
  19. package/dist/cli/extract-manifest.js +156 -117
  20. package/dist/index.cjs +329 -129
  21. package/dist/index.d.cts +26 -26
  22. package/dist/index.d.ts +26 -26
  23. package/dist/index.js +177 -118
  24. package/dist/{io-CfSPxeBN.d.ts → io-D4srzRKc.d.ts} +4 -6
  25. package/dist/protocols/mcp/index.cjs +750 -3
  26. package/dist/protocols/mcp/index.d.cts +7 -6
  27. package/dist/protocols/mcp/index.d.ts +7 -6
  28. package/dist/protocols/mcp/index.js +12 -2
  29. package/dist/protocols/oauth-metadata.cjs +539 -48
  30. package/dist/protocols/oauth-metadata.d.cts +6 -5
  31. package/dist/protocols/oauth-metadata.d.ts +6 -5
  32. package/dist/protocols/oauth-metadata.js +10 -52
  33. package/dist/protocols/rest/index.cjs +843 -5
  34. package/dist/protocols/rest/index.d.cts +10 -7
  35. package/dist/protocols/rest/index.d.ts +10 -7
  36. package/dist/protocols/rest/index.js +16 -3
  37. package/dist/stacks/supabase/index.cjs +1318 -76
  38. package/dist/stacks/supabase/index.d.cts +30 -29
  39. package/dist/stacks/supabase/index.d.ts +30 -29
  40. package/dist/stacks/supabase/index.js +89 -73
  41. package/dist/stacks/supabase/vite.cjs +287 -258
  42. package/dist/stacks/supabase/vite.d.cts +24 -24
  43. package/dist/stacks/supabase/vite.d.ts +24 -24
  44. package/dist/stacks/supabase/vite.js +252 -252
  45. package/dist/stacks/tanstack/index.cjs +1272 -42
  46. package/dist/stacks/tanstack/index.d.cts +9 -8
  47. package/dist/stacks/tanstack/index.d.ts +9 -8
  48. package/dist/stacks/tanstack/index.js +41 -36
  49. package/dist/stacks/tanstack/vite.cjs +312 -223
  50. package/dist/stacks/tanstack/vite.d.cts +81 -80
  51. package/dist/stacks/tanstack/vite.d.ts +81 -80
  52. package/dist/stacks/tanstack/vite.js +282 -216
  53. package/dist/types-qqrmJ736.d.ts +321 -0
  54. package/package.json +4 -4
  55. package/dist/authorize-CumVyk7p.d.cts +0 -27
  56. package/dist/authorize-D6p4-9_D.d.ts +0 -27
  57. package/dist/base-DCe0kP79.d.cts +0 -29
  58. package/dist/base-DCe0kP79.d.ts +0 -29
  59. package/dist/content-D5LJAHyM.js +0 -12
  60. package/dist/content-NCYIGgOb.cjs +0 -17
  61. package/dist/context-BweeEcoA.cjs +0 -56
  62. package/dist/context-CIpPzN7P.js +0 -51
  63. package/dist/cors-BxwVxCm3.cjs +0 -913
  64. package/dist/cors-CJRL7tyh.js +0 -830
  65. package/dist/forwarded--h4efJy-.js +0 -43
  66. package/dist/forwarded-Pi8Hs5Ps.cjs +0 -48
  67. package/dist/fs-errors-CWNzOV75.cjs +0 -11
  68. package/dist/fs-errors-PA2t1TIp.js +0 -6
  69. package/dist/io-DPYvBqET.d.cts +0 -15
  70. package/dist/list-tools-B4TTPEl1.d.cts +0 -8
  71. package/dist/list-tools-Duj1zT0T.js +0 -46
  72. package/dist/list-tools-_Hh_o_mD.cjs +0 -57
  73. package/dist/list-tools-bh6-eqZM.d.ts +0 -8
  74. package/dist/logger-2OXtfiIW.js +0 -141
  75. package/dist/logger-BqPOa_E1.cjs +0 -194
  76. package/dist/mcp--iewDKrV.cjs +0 -128
  77. package/dist/mcp-CALHyhDi.js +0 -123
  78. package/dist/metadata-path-CAkNcfyY.js +0 -12
  79. package/dist/metadata-path-zd7NSNoa.cjs +0 -17
  80. package/dist/package-CLTXgGyU.cjs +0 -9
  81. package/dist/package-g7-GHkOW.js +0 -4
  82. package/dist/paths-IS65L6TA.js +0 -7
  83. package/dist/paths-OP318a6c.cjs +0 -18
  84. package/dist/rest-BcS3SYjx.cjs +0 -122
  85. package/dist/rest-Dv9Xq9cV.js +0 -117
  86. package/dist/types-BYdYYTjk.d.cts +0 -321
  87. package/dist/types-BYdYYTjk.d.ts +0 -321
@@ -1,128 +0,0 @@
1
- const require_logger = require("./logger-BqPOa_E1.cjs");
2
- const require_context = require("./context-BweeEcoA.cjs");
3
- const require_cors = require("./cors-BxwVxCm3.cjs");
4
- const require_content = require("./content-NCYIGgOb.cjs");
5
- let _modelcontextprotocol_sdk_server_mcp_js = require("@modelcontextprotocol/sdk/server/mcp.js");
6
- let _modelcontextprotocol_sdk_server_webStandardStreamableHttp_js = require("@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js");
7
- //#region src/protocols/mcp/protocol.ts
8
- /**
9
- * Adapt a Lovable tool to the SDK's tool callback. The boundary cast bridges
10
- * our SDK-decoupled content-block types to the SDK's; they match structurally.
11
- */
12
- function adaptToolToSdkCallback(tool, auth, recorder) {
13
- const endUserId = auth?.principal.sub;
14
- return (async (first) => {
15
- const args = tool.inputSchema ? first ?? {} : {};
16
- const start = require_cors.nowMs();
17
- let result;
18
- try {
19
- result = await tool.handler(args, new require_context.ToolContext(auth));
20
- } catch {
21
- await recorder.emit({
22
- tool: tool.name,
23
- method: "tools/call",
24
- outcome: "handler_error",
25
- durationMs: require_cors.nowMs() - start,
26
- endUserId
27
- });
28
- return {
29
- content: [{
30
- type: "text",
31
- text: "tool execution failed"
32
- }],
33
- isError: true
34
- };
35
- }
36
- if (result == null) {
37
- await recorder.emit({
38
- tool: tool.name,
39
- method: "tools/call",
40
- outcome: "handler_error",
41
- durationMs: require_cors.nowMs() - start,
42
- endUserId
43
- });
44
- return {
45
- content: [{
46
- type: "text",
47
- text: `tool "${tool.name}" returned no result`
48
- }],
49
- isError: true
50
- };
51
- }
52
- await recorder.emit({
53
- tool: tool.name,
54
- method: "tools/call",
55
- outcome: result.isError ? "tool_error" : "ok",
56
- durationMs: require_cors.nowMs() - start,
57
- errorText: result.isError ? require_content.extractTextContent(result.content) : void 0,
58
- endUserId
59
- });
60
- return {
61
- content: result.content ?? [],
62
- structuredContent: result.structuredContent,
63
- isError: result.isError
64
- };
65
- });
66
- }
67
- /**
68
- * Build a Web-Standard request handler that serves the MCP protocol over
69
- * streamable HTTP. A fresh `McpServer` + transport is constructed per
70
- * request — the transport is stateless, so this is cheap and there is no
71
- * cross-request state to leak. The per-request `recorder` is supplied by the
72
- * caller (the stack handler) so telemetry reads the runtime env in-flight.
73
- */
74
- function createMcpProtocolHandler(mcp, options = {}) {
75
- const authorizer = require_cors.createRequestAuthorizer(mcp, options);
76
- const handle = async (request, recorder) => {
77
- const authResult = await authorizer.authorize(request, recorder);
78
- if (!authResult.ok) return authResult.response;
79
- try {
80
- const server = new _modelcontextprotocol_sdk_server_mcp_js.McpServer({
81
- name: mcp.name,
82
- version: mcp.version,
83
- title: mcp.title
84
- }, { instructions: mcp.instructions });
85
- for (const tool of mcp.tools) server.registerTool(tool.name, {
86
- title: tool.title,
87
- description: tool.description,
88
- inputSchema: tool.inputSchema,
89
- outputSchema: tool.outputSchema,
90
- annotations: tool.annotations
91
- }, adaptToolToSdkCallback(tool, authResult.auth, recorder));
92
- const transport = new _modelcontextprotocol_sdk_server_webStandardStreamableHttp_js.WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
93
- await server.connect(transport);
94
- return await transport.handleRequest(request);
95
- } catch (err) {
96
- await recorder.emit({
97
- tool: null,
98
- method: "transport",
99
- outcome: "transport_error",
100
- durationMs: 0,
101
- endUserId: authResult.auth?.principal.sub
102
- });
103
- require_logger.log.error("mcp.transport_error", {
104
- ...require_logger.describeError(err),
105
- outcome: "500 internal error"
106
- });
107
- return Response.json({
108
- jsonrpc: "2.0",
109
- id: null,
110
- error: {
111
- code: -32603,
112
- message: "internal error"
113
- }
114
- }, { status: 500 });
115
- }
116
- };
117
- return async (request, recorder = require_cors.createNoopRecorder()) => {
118
- if (request.method === "OPTIONS") return require_cors.corsPreflightResponse("GET, POST, DELETE, OPTIONS");
119
- return require_cors.withCors(await handle(request, recorder));
120
- };
121
- }
122
- //#endregion
123
- Object.defineProperty(exports, "createMcpProtocolHandler", {
124
- enumerable: true,
125
- get: function() {
126
- return createMcpProtocolHandler;
127
- }
128
- });
@@ -1,123 +0,0 @@
1
- import { i as log, r as describeError } from "./logger-2OXtfiIW.js";
2
- import { t as ToolContext } from "./context-CIpPzN7P.js";
3
- import { d as nowMs, i as createRequestAuthorizer, n as withCors, t as corsPreflightResponse, u as createNoopRecorder } from "./cors-CJRL7tyh.js";
4
- import { t as extractTextContent } from "./content-D5LJAHyM.js";
5
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
6
- import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
7
- //#region src/protocols/mcp/protocol.ts
8
- /**
9
- * Adapt a Lovable tool to the SDK's tool callback. The boundary cast bridges
10
- * our SDK-decoupled content-block types to the SDK's; they match structurally.
11
- */
12
- function adaptToolToSdkCallback(tool, auth, recorder) {
13
- const endUserId = auth?.principal.sub;
14
- return (async (first) => {
15
- const args = tool.inputSchema ? first ?? {} : {};
16
- const start = nowMs();
17
- let result;
18
- try {
19
- result = await tool.handler(args, new ToolContext(auth));
20
- } catch {
21
- await recorder.emit({
22
- tool: tool.name,
23
- method: "tools/call",
24
- outcome: "handler_error",
25
- durationMs: nowMs() - start,
26
- endUserId
27
- });
28
- return {
29
- content: [{
30
- type: "text",
31
- text: "tool execution failed"
32
- }],
33
- isError: true
34
- };
35
- }
36
- if (result == null) {
37
- await recorder.emit({
38
- tool: tool.name,
39
- method: "tools/call",
40
- outcome: "handler_error",
41
- durationMs: nowMs() - start,
42
- endUserId
43
- });
44
- return {
45
- content: [{
46
- type: "text",
47
- text: `tool "${tool.name}" returned no result`
48
- }],
49
- isError: true
50
- };
51
- }
52
- await recorder.emit({
53
- tool: tool.name,
54
- method: "tools/call",
55
- outcome: result.isError ? "tool_error" : "ok",
56
- durationMs: nowMs() - start,
57
- errorText: result.isError ? extractTextContent(result.content) : void 0,
58
- endUserId
59
- });
60
- return {
61
- content: result.content ?? [],
62
- structuredContent: result.structuredContent,
63
- isError: result.isError
64
- };
65
- });
66
- }
67
- /**
68
- * Build a Web-Standard request handler that serves the MCP protocol over
69
- * streamable HTTP. A fresh `McpServer` + transport is constructed per
70
- * request — the transport is stateless, so this is cheap and there is no
71
- * cross-request state to leak. The per-request `recorder` is supplied by the
72
- * caller (the stack handler) so telemetry reads the runtime env in-flight.
73
- */
74
- function createMcpProtocolHandler(mcp, options = {}) {
75
- const authorizer = createRequestAuthorizer(mcp, options);
76
- const handle = async (request, recorder) => {
77
- const authResult = await authorizer.authorize(request, recorder);
78
- if (!authResult.ok) return authResult.response;
79
- try {
80
- const server = new McpServer({
81
- name: mcp.name,
82
- version: mcp.version,
83
- title: mcp.title
84
- }, { instructions: mcp.instructions });
85
- for (const tool of mcp.tools) server.registerTool(tool.name, {
86
- title: tool.title,
87
- description: tool.description,
88
- inputSchema: tool.inputSchema,
89
- outputSchema: tool.outputSchema,
90
- annotations: tool.annotations
91
- }, adaptToolToSdkCallback(tool, authResult.auth, recorder));
92
- const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
93
- await server.connect(transport);
94
- return await transport.handleRequest(request);
95
- } catch (err) {
96
- await recorder.emit({
97
- tool: null,
98
- method: "transport",
99
- outcome: "transport_error",
100
- durationMs: 0,
101
- endUserId: authResult.auth?.principal.sub
102
- });
103
- log.error("mcp.transport_error", {
104
- ...describeError(err),
105
- outcome: "500 internal error"
106
- });
107
- return Response.json({
108
- jsonrpc: "2.0",
109
- id: null,
110
- error: {
111
- code: -32603,
112
- message: "internal error"
113
- }
114
- }, { status: 500 });
115
- }
116
- };
117
- return async (request, recorder = createNoopRecorder()) => {
118
- if (request.method === "OPTIONS") return corsPreflightResponse("GET, POST, DELETE, OPTIONS");
119
- return withCors(await handle(request, recorder));
120
- };
121
- }
122
- //#endregion
123
- export { createMcpProtocolHandler as t };
@@ -1,12 +0,0 @@
1
- //#region src/auth/metadata-path.ts
2
- /**
3
- * Default RFC 9728 protected-resource metadata path. Used by the runtime 401
4
- * challenge (`authorize.ts`, which advertises this URL via the
5
- * `WWW-Authenticate: resource_metadata` param) and the Vite plugin (which emits
6
- * the route that serves it) when no `metadataPath` is configured. A configured
7
- * `metadataPath` overrides it; the plugin injects that same path into every
8
- * emitted handler, so the advertised URL and the served route still can't drift.
9
- */
10
- const OAUTH_PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
11
- //#endregion
12
- export { OAUTH_PROTECTED_RESOURCE_METADATA_PATH as t };
@@ -1,17 +0,0 @@
1
- //#region src/auth/metadata-path.ts
2
- /**
3
- * Default RFC 9728 protected-resource metadata path. Used by the runtime 401
4
- * challenge (`authorize.ts`, which advertises this URL via the
5
- * `WWW-Authenticate: resource_metadata` param) and the Vite plugin (which emits
6
- * the route that serves it) when no `metadataPath` is configured. A configured
7
- * `metadataPath` overrides it; the plugin injects that same path into every
8
- * emitted handler, so the advertised URL and the served route still can't drift.
9
- */
10
- const OAUTH_PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
11
- //#endregion
12
- Object.defineProperty(exports, "OAUTH_PROTECTED_RESOURCE_METADATA_PATH", {
13
- enumerable: true,
14
- get: function() {
15
- return OAUTH_PROTECTED_RESOURCE_METADATA_PATH;
16
- }
17
- });
@@ -1,9 +0,0 @@
1
- //#region package.json
2
- var version = "0.22.1-rc.1";
3
- //#endregion
4
- Object.defineProperty(exports, "version", {
5
- enumerable: true,
6
- get: function() {
7
- return version;
8
- }
9
- });
@@ -1,4 +0,0 @@
1
- //#region package.json
2
- var version = "0.22.1-rc.1";
3
- //#endregion
4
- export { version as t };
@@ -1,7 +0,0 @@
1
- //#region src/stacks/supabase/paths.ts
2
- const FUNCTIONS_MOUNT_PREFIX = "/functions/v1/";
3
- function assertFunctionName(value, label = "functionName") {
4
- if (!/^[a-zA-Z0-9_-]+$/.test(value)) throw new Error(`@lovable.dev/mcp-js: ${label} must be a single path segment matching [A-Za-z0-9_-], got ${JSON.stringify(value)}`);
5
- }
6
- //#endregion
7
- export { assertFunctionName as n, FUNCTIONS_MOUNT_PREFIX as t };
@@ -1,18 +0,0 @@
1
- //#region src/stacks/supabase/paths.ts
2
- const FUNCTIONS_MOUNT_PREFIX = "/functions/v1/";
3
- function assertFunctionName(value, label = "functionName") {
4
- if (!/^[a-zA-Z0-9_-]+$/.test(value)) throw new Error(`@lovable.dev/mcp-js: ${label} must be a single path segment matching [A-Za-z0-9_-], got ${JSON.stringify(value)}`);
5
- }
6
- //#endregion
7
- Object.defineProperty(exports, "FUNCTIONS_MOUNT_PREFIX", {
8
- enumerable: true,
9
- get: function() {
10
- return FUNCTIONS_MOUNT_PREFIX;
11
- }
12
- });
13
- Object.defineProperty(exports, "assertFunctionName", {
14
- enumerable: true,
15
- get: function() {
16
- return assertFunctionName;
17
- }
18
- });
@@ -1,122 +0,0 @@
1
- const require_context = require("./context-BweeEcoA.cjs");
2
- const require_cors = require("./cors-BxwVxCm3.cjs");
3
- const require_content = require("./content-NCYIGgOb.cjs");
4
- require("./list-tools-_Hh_o_mD.cjs");
5
- let _modelcontextprotocol_sdk_server_zod_compat_js = require("@modelcontextprotocol/sdk/server/zod-compat.js");
6
- //#region src/protocols/rest/invoke-tool.ts
7
- const MAX_REFLECTED_TOOL_NAME = 256;
8
- function safeReflectName(name) {
9
- const text = String(name);
10
- return text.length > MAX_REFLECTED_TOOL_NAME ? `${text.slice(0, MAX_REFLECTED_TOOL_NAME)}…` : text;
11
- }
12
- function isEmptyArgs(value) {
13
- if (value == null) return true;
14
- if (typeof value !== "object" || Array.isArray(value)) return false;
15
- return Object.keys(value).length === 0;
16
- }
17
- function createInvokeToolHandler(mcp, options = {}) {
18
- require_cors.assertRestResourceBinding(mcp, options);
19
- const authorizer = require_cors.createRequestAuthorizer(mcp, options);
20
- const handle = async (request, toolName, recorder) => {
21
- const authResult = await authorizer.authorize(request, recorder);
22
- if (!authResult.ok) return authResult.response;
23
- const endUserId = authResult.auth?.principal.sub;
24
- if (request.method !== "POST") return require_cors.methodNotAllowed("POST, OPTIONS");
25
- const tool = mcp.tools.find((t) => t.name === toolName);
26
- if (!tool) return new Response(JSON.stringify({ error: `unknown tool: ${safeReflectName(toolName)}` }), {
27
- status: 404,
28
- headers: require_cors.JSON_HEADERS
29
- });
30
- let rawArgs = {};
31
- const text = await request.text();
32
- if (text) try {
33
- rawArgs = JSON.parse(text);
34
- } catch {
35
- return new Response(JSON.stringify({ error: "invalid JSON body" }), {
36
- status: 400,
37
- headers: require_cors.JSON_HEADERS
38
- });
39
- }
40
- let args = rawArgs;
41
- if (tool.inputSchema) try {
42
- const parsed = await (0, _modelcontextprotocol_sdk_server_zod_compat_js.safeParseAsync)((0, _modelcontextprotocol_sdk_server_zod_compat_js.objectFromShape)(tool.inputSchema), rawArgs);
43
- if (!parsed.success) return new Response(JSON.stringify({
44
- error: "validation failed",
45
- details: (0, _modelcontextprotocol_sdk_server_zod_compat_js.getParseErrorMessage)(parsed.error)
46
- }), {
47
- status: 400,
48
- headers: require_cors.JSON_HEADERS
49
- });
50
- args = parsed.data;
51
- } catch {
52
- return new Response(JSON.stringify({
53
- error: "schema error",
54
- tool: toolName
55
- }), {
56
- status: 500,
57
- headers: require_cors.JSON_HEADERS
58
- });
59
- }
60
- else if (!isEmptyArgs(rawArgs)) return new Response(JSON.stringify({ error: "tool has no inputSchema; expected empty body" }), {
61
- status: 400,
62
- headers: require_cors.JSON_HEADERS
63
- });
64
- let result;
65
- const start = require_cors.nowMs();
66
- try {
67
- result = await tool.handler(args, new require_context.ToolContext(authResult.auth));
68
- } catch {
69
- await recorder.emit({
70
- tool: tool.name,
71
- method: "tools/call",
72
- outcome: "handler_error",
73
- durationMs: require_cors.nowMs() - start,
74
- endUserId
75
- });
76
- return new Response(JSON.stringify({
77
- error: "handler threw",
78
- tool: toolName
79
- }), {
80
- status: 500,
81
- headers: require_cors.JSON_HEADERS
82
- });
83
- }
84
- if (result == null) {
85
- await recorder.emit({
86
- tool: tool.name,
87
- method: "tools/call",
88
- outcome: "handler_error",
89
- durationMs: require_cors.nowMs() - start,
90
- endUserId
91
- });
92
- return new Response(JSON.stringify({ error: `tool "${toolName}" returned no result` }), {
93
- status: 500,
94
- headers: require_cors.JSON_HEADERS
95
- });
96
- }
97
- await recorder.emit({
98
- tool: tool.name,
99
- method: "tools/call",
100
- outcome: result.isError ? "tool_error" : "ok",
101
- durationMs: require_cors.nowMs() - start,
102
- errorText: result.isError ? require_content.extractTextContent(result.content) : void 0,
103
- endUserId
104
- });
105
- return Response.json({
106
- content: result.content ?? [],
107
- structuredContent: result.structuredContent,
108
- isError: result.isError
109
- });
110
- };
111
- return async (request, toolName, recorder = require_cors.createNoopRecorder()) => {
112
- if (request.method === "OPTIONS") return require_cors.corsPreflightResponse("POST, OPTIONS");
113
- return require_cors.withCors(await handle(request, toolName, recorder));
114
- };
115
- }
116
- //#endregion
117
- Object.defineProperty(exports, "createInvokeToolHandler", {
118
- enumerable: true,
119
- get: function() {
120
- return createInvokeToolHandler;
121
- }
122
- });
@@ -1,117 +0,0 @@
1
- import { t as ToolContext } from "./context-CIpPzN7P.js";
2
- import { d as nowMs, f as JSON_HEADERS, i as createRequestAuthorizer, m as methodNotAllowed, n as withCors, r as assertRestResourceBinding, t as corsPreflightResponse, u as createNoopRecorder } from "./cors-CJRL7tyh.js";
3
- import { t as extractTextContent } from "./content-D5LJAHyM.js";
4
- import "./list-tools-Duj1zT0T.js";
5
- import { getParseErrorMessage, objectFromShape, safeParseAsync } from "@modelcontextprotocol/sdk/server/zod-compat.js";
6
- //#region src/protocols/rest/invoke-tool.ts
7
- const MAX_REFLECTED_TOOL_NAME = 256;
8
- function safeReflectName(name) {
9
- const text = String(name);
10
- return text.length > MAX_REFLECTED_TOOL_NAME ? `${text.slice(0, MAX_REFLECTED_TOOL_NAME)}…` : text;
11
- }
12
- function isEmptyArgs(value) {
13
- if (value == null) return true;
14
- if (typeof value !== "object" || Array.isArray(value)) return false;
15
- return Object.keys(value).length === 0;
16
- }
17
- function createInvokeToolHandler(mcp, options = {}) {
18
- assertRestResourceBinding(mcp, options);
19
- const authorizer = createRequestAuthorizer(mcp, options);
20
- const handle = async (request, toolName, recorder) => {
21
- const authResult = await authorizer.authorize(request, recorder);
22
- if (!authResult.ok) return authResult.response;
23
- const endUserId = authResult.auth?.principal.sub;
24
- if (request.method !== "POST") return methodNotAllowed("POST, OPTIONS");
25
- const tool = mcp.tools.find((t) => t.name === toolName);
26
- if (!tool) return new Response(JSON.stringify({ error: `unknown tool: ${safeReflectName(toolName)}` }), {
27
- status: 404,
28
- headers: JSON_HEADERS
29
- });
30
- let rawArgs = {};
31
- const text = await request.text();
32
- if (text) try {
33
- rawArgs = JSON.parse(text);
34
- } catch {
35
- return new Response(JSON.stringify({ error: "invalid JSON body" }), {
36
- status: 400,
37
- headers: JSON_HEADERS
38
- });
39
- }
40
- let args = rawArgs;
41
- if (tool.inputSchema) try {
42
- const parsed = await safeParseAsync(objectFromShape(tool.inputSchema), rawArgs);
43
- if (!parsed.success) return new Response(JSON.stringify({
44
- error: "validation failed",
45
- details: getParseErrorMessage(parsed.error)
46
- }), {
47
- status: 400,
48
- headers: JSON_HEADERS
49
- });
50
- args = parsed.data;
51
- } catch {
52
- return new Response(JSON.stringify({
53
- error: "schema error",
54
- tool: toolName
55
- }), {
56
- status: 500,
57
- headers: JSON_HEADERS
58
- });
59
- }
60
- else if (!isEmptyArgs(rawArgs)) return new Response(JSON.stringify({ error: "tool has no inputSchema; expected empty body" }), {
61
- status: 400,
62
- headers: JSON_HEADERS
63
- });
64
- let result;
65
- const start = nowMs();
66
- try {
67
- result = await tool.handler(args, new ToolContext(authResult.auth));
68
- } catch {
69
- await recorder.emit({
70
- tool: tool.name,
71
- method: "tools/call",
72
- outcome: "handler_error",
73
- durationMs: nowMs() - start,
74
- endUserId
75
- });
76
- return new Response(JSON.stringify({
77
- error: "handler threw",
78
- tool: toolName
79
- }), {
80
- status: 500,
81
- headers: JSON_HEADERS
82
- });
83
- }
84
- if (result == null) {
85
- await recorder.emit({
86
- tool: tool.name,
87
- method: "tools/call",
88
- outcome: "handler_error",
89
- durationMs: nowMs() - start,
90
- endUserId
91
- });
92
- return new Response(JSON.stringify({ error: `tool "${toolName}" returned no result` }), {
93
- status: 500,
94
- headers: JSON_HEADERS
95
- });
96
- }
97
- await recorder.emit({
98
- tool: tool.name,
99
- method: "tools/call",
100
- outcome: result.isError ? "tool_error" : "ok",
101
- durationMs: nowMs() - start,
102
- errorText: result.isError ? extractTextContent(result.content) : void 0,
103
- endUserId
104
- });
105
- return Response.json({
106
- content: result.content ?? [],
107
- structuredContent: result.structuredContent,
108
- isError: result.isError
109
- });
110
- };
111
- return async (request, toolName, recorder = createNoopRecorder()) => {
112
- if (request.method === "OPTIONS") return corsPreflightResponse("POST, OPTIONS");
113
- return withCors(await handle(request, toolName, recorder));
114
- };
115
- }
116
- //#endregion
117
- export { createInvokeToolHandler as t };