@jaypie/mcp 0.1.5 → 0.1.6

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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Finlayson Studio, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.js CHANGED
@@ -132,7 +132,7 @@ const DEFAULT_VERSION = "0.0.0";
132
132
  * Creates an Express handler for the Jaypie MCP server using HTTP transport
133
133
  *
134
134
  * @param options - Configuration options for the handler
135
- * @returns Express middleware function
135
+ * @returns Promise that resolves to an Express middleware function
136
136
  *
137
137
  * @example
138
138
  * ```typescript
@@ -142,7 +142,8 @@ const DEFAULT_VERSION = "0.0.0";
142
142
  * const app = express();
143
143
  * app.use(express.json());
144
144
  *
145
- * app.use('/mcp', mcpExpressHandler({
145
+ * // Note: mcpExpressHandler is now async
146
+ * app.use('/mcp', await mcpExpressHandler({
146
147
  * version: '1.0.0',
147
148
  * enableSessions: true
148
149
  * }));
@@ -152,14 +153,14 @@ const DEFAULT_VERSION = "0.0.0";
152
153
  * });
153
154
  * ```
154
155
  */
155
- function mcpExpressHandler(options = {}) {
156
+ async function mcpExpressHandler(options = {}) {
156
157
  const { version = DEFAULT_VERSION, enableSessions = true, sessionIdGenerator = () => randomUUID(), enableJsonResponse = false, } = options;
157
158
  const server = createMcpServer(version);
158
159
  const transport = new StreamableHTTPServerTransport({
159
160
  sessionIdGenerator: enableSessions ? sessionIdGenerator : undefined,
160
161
  enableJsonResponse,
161
162
  });
162
- server.connect(transport);
163
+ await server.connect(transport);
163
164
  return async (req, res) => {
164
165
  try {
165
166
  await transport.handleRequest(req, res, req.body);
@@ -177,10 +178,43 @@ function mcpExpressHandler(options = {}) {
177
178
 
178
179
  // Version will be injected during build
179
180
  const version = "0.0.0";
181
+ let server = null;
182
+ let isShuttingDown = false;
183
+ async function gracefulShutdown(exitCode = 0) {
184
+ if (isShuttingDown) {
185
+ return;
186
+ }
187
+ isShuttingDown = true;
188
+ try {
189
+ if (server) {
190
+ await server.close();
191
+ }
192
+ }
193
+ catch (error) {
194
+ // Ignore errors during shutdown
195
+ }
196
+ finally {
197
+ process.exit(exitCode);
198
+ }
199
+ }
180
200
  async function main() {
181
- const server = createMcpServer(version);
201
+ server = createMcpServer(version);
182
202
  const transport = new StdioServerTransport();
183
203
  await server.connect(transport);
204
+ // Handle process termination signals
205
+ process.on("SIGINT", () => gracefulShutdown(0));
206
+ process.on("SIGTERM", () => gracefulShutdown(0));
207
+ // Handle stdio stream errors (but let transport handle normal stdin end/close)
208
+ process.stdin.on("error", (error) => {
209
+ if (error.message?.includes("EPIPE") || error.message?.includes("EOF")) {
210
+ gracefulShutdown(0);
211
+ }
212
+ });
213
+ process.stdout.on("error", (error) => {
214
+ if (error.message?.includes("EPIPE")) {
215
+ gracefulShutdown(0);
216
+ }
217
+ });
184
218
  // Server is running on stdio
185
219
  }
186
220
  // Only run main() if this module is executed directly (not imported)
@@ -189,7 +223,10 @@ if (process.argv[1]) {
189
223
  const realPath = realpathSync(process.argv[1]);
190
224
  const realPathAsUrl = pathToFileURL(realPath).href;
191
225
  if (import.meta.url === realPathAsUrl) {
192
- main();
226
+ main().catch((error) => {
227
+ console.error("Fatal error:", error);
228
+ process.exit(1);
229
+ });
193
230
  }
194
231
  }
195
232
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/createMcpServer.ts","../src/mcpExpressHandler.ts","../src/index.ts"],"sourcesContent":["import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport matter from \"gray-matter\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\nconst PROMPTS_PATH = path.join(__dirname, \"..\", \"prompts\");\n\ninterface FrontMatter {\n description?: string;\n include?: string;\n globs?: string;\n}\n\nasync function parseMarkdownFile(filePath: string): Promise<{\n filename: string;\n description?: string;\n include?: string;\n}> {\n try {\n const content = await fs.readFile(filePath, \"utf-8\");\n const filename = path.basename(filePath);\n\n if (content.startsWith(\"---\")) {\n const parsed = matter(content);\n const frontMatter = parsed.data as FrontMatter;\n return {\n filename,\n description: frontMatter.description,\n include: frontMatter.include || frontMatter.globs,\n };\n }\n\n return { filename };\n } catch {\n return { filename: path.basename(filePath) };\n }\n}\n\nfunction formatPromptListItem(prompt: {\n filename: string;\n description?: string;\n include?: string;\n}): string {\n const { filename, description, include } = prompt;\n\n if (description && include) {\n return `* ${filename}: ${description} - Required for ${include}`;\n } else if (description) {\n return `* ${filename}: ${description}`;\n } else if (include) {\n return `* ${filename} - Required for ${include}`;\n } else {\n return `* ${filename}`;\n }\n}\n\n/**\n * Creates and configures an MCP server instance with Jaypie tools\n * @param version - The version string for the server\n * @returns Configured MCP server instance\n */\nexport function createMcpServer(version: string = \"0.0.0\"): McpServer {\n const server = new McpServer(\n {\n name: \"jaypie\",\n version,\n },\n {\n capabilities: {},\n },\n );\n\n server.tool(\n \"list_prompts\",\n \"Returns a bulleted list of all .md files in the prompts directory with their descriptions and requirements\",\n {},\n async () => {\n try {\n const files = await fs.readdir(PROMPTS_PATH);\n const mdFiles = files.filter((file) => file.endsWith(\".md\"));\n\n const prompts = await Promise.all(\n mdFiles.map((file) =>\n parseMarkdownFile(path.join(PROMPTS_PATH, file)),\n ),\n );\n\n const formattedList = prompts.map(formatPromptListItem).join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n formattedList || \"No .md files found in the prompts directory.\",\n },\n ],\n };\n } catch (error) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Error listing prompts: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n },\n ],\n };\n }\n },\n );\n\n server.tool(\n \"read_prompt\",\n \"Returns the contents of a specified prompt file\",\n {\n filename: z\n .string()\n .describe(\n \"The name of the prompt file to read (e.g., example_prompt.md)\",\n ),\n },\n async ({ filename }) => {\n try {\n const filePath = path.join(PROMPTS_PATH, filename);\n const content = await fs.readFile(filePath, \"utf-8\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: content,\n },\n ],\n };\n } catch (error) {\n if ((error as { code?: string }).code === \"ENOENT\") {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Error: Prompt file \"${filename}\" not found in prompts directory`,\n },\n ],\n };\n }\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Error reading prompt file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n },\n ],\n };\n }\n },\n );\n\n return server;\n}\n","import { Request, Response } from \"express\";\nimport { StreamableHTTPServerTransport } from \"@modelcontextprotocol/sdk/server/streamableHttp.js\";\nimport { randomUUID } from \"node:crypto\";\nimport { createMcpServer } from \"./createMcpServer.js\";\n\n// Version will be injected during build\nconst DEFAULT_VERSION = \"0.0.0\";\n\n/**\n * Options for configuring the MCP Express handler\n */\nexport interface McpExpressHandlerOptions {\n /**\n * Version string for the MCP server\n */\n version?: string;\n /**\n * Whether to enable session management (stateful mode)\n * Default: true\n */\n enableSessions?: boolean;\n /**\n * Custom session ID generator function\n */\n sessionIdGenerator?: () => string;\n /**\n * Enable JSON responses instead of SSE streaming\n * Default: false\n */\n enableJsonResponse?: boolean;\n}\n\n/**\n * Creates an Express handler for the Jaypie MCP server using HTTP transport\n *\n * @param options - Configuration options for the handler\n * @returns Express middleware function\n *\n * @example\n * ```typescript\n * import express from 'express';\n * import { mcpExpressHandler } from '@jaypie/mcp';\n *\n * const app = express();\n * app.use(express.json());\n *\n * app.use('/mcp', mcpExpressHandler({\n * version: '1.0.0',\n * enableSessions: true\n * }));\n *\n * app.listen(3000, () => {\n * console.log('MCP server running on http://localhost:3000/mcp');\n * });\n * ```\n */\nexport function mcpExpressHandler(\n options: McpExpressHandlerOptions = {},\n): (req: Request, res: Response) => Promise<void> {\n const {\n version = DEFAULT_VERSION,\n enableSessions = true,\n sessionIdGenerator = () => randomUUID(),\n enableJsonResponse = false,\n } = options;\n\n const server = createMcpServer(version);\n\n const transport = new StreamableHTTPServerTransport({\n sessionIdGenerator: enableSessions ? sessionIdGenerator : undefined,\n enableJsonResponse,\n });\n\n server.connect(transport);\n\n return async (req: Request, res: Response): Promise<void> => {\n try {\n await transport.handleRequest(req, res, req.body);\n } catch (error) {\n if (!res.headersSent) {\n res.status(500).json({\n error: \"Internal server error\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n });\n }\n }\n };\n}\n","#!/usr/bin/env node\nimport { realpathSync } from \"node:fs\";\nimport { pathToFileURL } from \"node:url\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { createMcpServer } from \"./createMcpServer.js\";\n\n// Version will be injected during build\nconst version = \"0.0.0\";\n\nasync function main() {\n const server = createMcpServer(version);\n const transport = new StdioServerTransport();\n await server.connect(transport);\n // Server is running on stdio\n}\n\n// Only run main() if this module is executed directly (not imported)\n// This handles npx and symlinks in node_modules/.bin correctly\nif (process.argv[1]) {\n const realPath = realpathSync(process.argv[1]);\n const realPathAsUrl = pathToFileURL(realPath).href;\n if (import.meta.url === realPathAsUrl) {\n main();\n }\n}\n\nexport { createMcpServer } from \"./createMcpServer.js\";\nexport {\n mcpExpressHandler,\n type McpExpressHandlerOptions,\n} from \"./mcpExpressHandler.js\";\n"],"names":[],"mappings":";;;;;;;;;;;;AAOA,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;AACjD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC;AAC1C,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC;AAQ1D,eAAe,iBAAiB,CAAC,QAAgB,EAAA;AAK/C,IAAA,IAAI;QACF,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;QACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC;AAExC,QAAA,IAAI,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;AAC7B,YAAA,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;AAC9B,YAAA,MAAM,WAAW,GAAG,MAAM,CAAC,IAAmB;YAC9C,OAAO;gBACL,QAAQ;gBACR,WAAW,EAAE,WAAW,CAAC,WAAW;AACpC,gBAAA,OAAO,EAAE,WAAW,CAAC,OAAO,IAAI,WAAW,CAAC,KAAK;aAClD;QACH;QAEA,OAAO,EAAE,QAAQ,EAAE;IACrB;AAAE,IAAA,MAAM;QACN,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;IAC9C;AACF;AAEA,SAAS,oBAAoB,CAAC,MAI7B,EAAA;IACC,MAAM,EAAE,QAAQ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EvB,eAAe,IAAI,GAAA;AACjB,IAAA,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC;AACvC,IAAA,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE;AAC5C,IAAA,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC;;AAEjC;AAEA;AACA;AACA,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;IACnB,MAAM,QAAQ,GAAG,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9C,MAAM,aAAa,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,IAAI;IAClD,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,aAAa,EAAE;AACrC,QAAA,IAAI,EAAE;IACR;AACF;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/createMcpServer.ts","../src/mcpExpressHandler.ts","../src/index.ts"],"sourcesContent":["import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport matter from \"gray-matter\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\nconst PROMPTS_PATH = path.join(__dirname, \"..\", \"prompts\");\n\ninterface FrontMatter {\n description?: string;\n include?: string;\n globs?: string;\n}\n\nasync function parseMarkdownFile(filePath: string): Promise<{\n filename: string;\n description?: string;\n include?: string;\n}> {\n try {\n const content = await fs.readFile(filePath, \"utf-8\");\n const filename = path.basename(filePath);\n\n if (content.startsWith(\"---\")) {\n const parsed = matter(content);\n const frontMatter = parsed.data as FrontMatter;\n return {\n filename,\n description: frontMatter.description,\n include: frontMatter.include || frontMatter.globs,\n };\n }\n\n return { filename };\n } catch {\n return { filename: path.basename(filePath) };\n }\n}\n\nfunction formatPromptListItem(prompt: {\n filename: string;\n description?: string;\n include?: string;\n}): string {\n const { filename, description, include } = prompt;\n\n if (description && include) {\n return `* ${filename}: ${description} - Required for ${include}`;\n } else if (description) {\n return `* ${filename}: ${description}`;\n } else if (include) {\n return `* ${filename} - Required for ${include}`;\n } else {\n return `* ${filename}`;\n }\n}\n\n/**\n * Creates and configures an MCP server instance with Jaypie tools\n * @param version - The version string for the server\n * @returns Configured MCP server instance\n */\nexport function createMcpServer(version: string = \"0.0.0\"): McpServer {\n const server = new McpServer(\n {\n name: \"jaypie\",\n version,\n },\n {\n capabilities: {},\n },\n );\n\n server.tool(\n \"list_prompts\",\n \"Returns a bulleted list of all .md files in the prompts directory with their descriptions and requirements\",\n {},\n async () => {\n try {\n const files = await fs.readdir(PROMPTS_PATH);\n const mdFiles = files.filter((file) => file.endsWith(\".md\"));\n\n const prompts = await Promise.all(\n mdFiles.map((file) =>\n parseMarkdownFile(path.join(PROMPTS_PATH, file)),\n ),\n );\n\n const formattedList = prompts.map(formatPromptListItem).join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n formattedList || \"No .md files found in the prompts directory.\",\n },\n ],\n };\n } catch (error) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Error listing prompts: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n },\n ],\n };\n }\n },\n );\n\n server.tool(\n \"read_prompt\",\n \"Returns the contents of a specified prompt file\",\n {\n filename: z\n .string()\n .describe(\n \"The name of the prompt file to read (e.g., example_prompt.md)\",\n ),\n },\n async ({ filename }) => {\n try {\n const filePath = path.join(PROMPTS_PATH, filename);\n const content = await fs.readFile(filePath, \"utf-8\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: content,\n },\n ],\n };\n } catch (error) {\n if ((error as { code?: string }).code === \"ENOENT\") {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Error: Prompt file \"${filename}\" not found in prompts directory`,\n },\n ],\n };\n }\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Error reading prompt file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n },\n ],\n };\n }\n },\n );\n\n return server;\n}\n","import { Request, Response } from \"express\";\nimport { StreamableHTTPServerTransport } from \"@modelcontextprotocol/sdk/server/streamableHttp.js\";\nimport { randomUUID } from \"node:crypto\";\nimport { createMcpServer } from \"./createMcpServer.js\";\n\n// Version will be injected during build\nconst DEFAULT_VERSION = \"0.0.0\";\n\n/**\n * Options for configuring the MCP Express handler\n */\nexport interface McpExpressHandlerOptions {\n /**\n * Version string for the MCP server\n */\n version?: string;\n /**\n * Whether to enable session management (stateful mode)\n * Default: true\n */\n enableSessions?: boolean;\n /**\n * Custom session ID generator function\n */\n sessionIdGenerator?: () => string;\n /**\n * Enable JSON responses instead of SSE streaming\n * Default: false\n */\n enableJsonResponse?: boolean;\n}\n\n/**\n * Creates an Express handler for the Jaypie MCP server using HTTP transport\n *\n * @param options - Configuration options for the handler\n * @returns Promise that resolves to an Express middleware function\n *\n * @example\n * ```typescript\n * import express from 'express';\n * import { mcpExpressHandler } from '@jaypie/mcp';\n *\n * const app = express();\n * app.use(express.json());\n *\n * // Note: mcpExpressHandler is now async\n * app.use('/mcp', await mcpExpressHandler({\n * version: '1.0.0',\n * enableSessions: true\n * }));\n *\n * app.listen(3000, () => {\n * console.log('MCP server running on http://localhost:3000/mcp');\n * });\n * ```\n */\nexport async function mcpExpressHandler(\n options: McpExpressHandlerOptions = {},\n): Promise<(req: Request, res: Response) => Promise<void>> {\n const {\n version = DEFAULT_VERSION,\n enableSessions = true,\n sessionIdGenerator = () => randomUUID(),\n enableJsonResponse = false,\n } = options;\n\n const server = createMcpServer(version);\n\n const transport = new StreamableHTTPServerTransport({\n sessionIdGenerator: enableSessions ? sessionIdGenerator : undefined,\n enableJsonResponse,\n });\n\n await server.connect(transport);\n\n return async (req: Request, res: Response): Promise<void> => {\n try {\n await transport.handleRequest(req, res, req.body);\n } catch (error) {\n if (!res.headersSent) {\n res.status(500).json({\n error: \"Internal server error\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n });\n }\n }\n };\n}\n","#!/usr/bin/env node\nimport { realpathSync } from \"node:fs\";\nimport { pathToFileURL } from \"node:url\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { createMcpServer } from \"./createMcpServer.js\";\n\n// Version will be injected during build\nconst version = \"0.0.0\";\n\nlet server: McpServer | null = null;\nlet isShuttingDown = false;\n\nasync function gracefulShutdown(exitCode = 0) {\n if (isShuttingDown) {\n return;\n }\n isShuttingDown = true;\n\n try {\n if (server) {\n await server.close();\n }\n } catch (error) {\n // Ignore errors during shutdown\n } finally {\n process.exit(exitCode);\n }\n}\n\nasync function main() {\n server = createMcpServer(version);\n const transport = new StdioServerTransport();\n await server.connect(transport);\n\n // Handle process termination signals\n process.on(\"SIGINT\", () => gracefulShutdown(0));\n process.on(\"SIGTERM\", () => gracefulShutdown(0));\n\n // Handle stdio stream errors (but let transport handle normal stdin end/close)\n process.stdin.on(\"error\", (error) => {\n if (error.message?.includes(\"EPIPE\") || error.message?.includes(\"EOF\")) {\n gracefulShutdown(0);\n }\n });\n\n process.stdout.on(\"error\", (error) => {\n if (error.message?.includes(\"EPIPE\")) {\n gracefulShutdown(0);\n }\n });\n\n // Server is running on stdio\n}\n\n// Only run main() if this module is executed directly (not imported)\n// This handles npx and symlinks in node_modules/.bin correctly\nif (process.argv[1]) {\n const realPath = realpathSync(process.argv[1]);\n const realPathAsUrl = pathToFileURL(realPath).href;\n if (import.meta.url === realPathAsUrl) {\n main().catch((error) => {\n console.error(\"Fatal error:\", error);\n process.exit(1);\n });\n }\n}\n\nexport { createMcpServer } from \"./createMcpServer.js\";\nexport {\n mcpExpressHandler,\n type McpExpressHandlerOptions,\n} from \"./mcpExpressHandler.js\";\n"],"names":[],"mappings":";;;;;;;;;;;;AAOA,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;AACjD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO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,CAAC;IACJ;AACF;;;;"}
@@ -26,7 +26,7 @@ export interface McpExpressHandlerOptions {
26
26
  * Creates an Express handler for the Jaypie MCP server using HTTP transport
27
27
  *
28
28
  * @param options - Configuration options for the handler
29
- * @returns Express middleware function
29
+ * @returns Promise that resolves to an Express middleware function
30
30
  *
31
31
  * @example
32
32
  * ```typescript
@@ -36,7 +36,8 @@ export interface McpExpressHandlerOptions {
36
36
  * const app = express();
37
37
  * app.use(express.json());
38
38
  *
39
- * app.use('/mcp', mcpExpressHandler({
39
+ * // Note: mcpExpressHandler is now async
40
+ * app.use('/mcp', await mcpExpressHandler({
40
41
  * version: '1.0.0',
41
42
  * enableSessions: true
42
43
  * }));
@@ -46,4 +47,4 @@ export interface McpExpressHandlerOptions {
46
47
  * });
47
48
  * ```
48
49
  */
49
- export declare function mcpExpressHandler(options?: McpExpressHandlerOptions): (req: Request, res: Response) => Promise<void>;
50
+ export declare function mcpExpressHandler(options?: McpExpressHandlerOptions): Promise<(req: Request, res: Response) => Promise<void>>;
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@jaypie/mcp",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Jaypie MCP server",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/finlaysonstudio/jaypie"
8
+ },
5
9
  "license": "MIT",
6
10
  "author": "Finlayson Studio",
7
11
  "type": "module",
@@ -41,5 +45,6 @@
41
45
  },
42
46
  "publishConfig": {
43
47
  "access": "public"
44
- }
48
+ },
49
+ "gitHead": "4d7e735fac6495df0dbbc405f53e9d4113095cc6"
45
50
  }