@cyanheads/pubmed-mcp-server 1.2.1 → 1.2.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.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # PubMed MCP Server
2
2
 
3
3
  [![TypeScript](https://img.shields.io/badge/TypeScript-^5.8.3-blue.svg)](https://www.typescriptlang.org/)
4
- [![Model Context Protocol](https://img.shields.io/badge/MCP%20SDK-^1.12.1-green.svg)](https://modelcontextprotocol.io/)
5
- [![Version](https://img.shields.io/badge/Version-1.2.1-blue.svg)](./CHANGELOG.md)
4
+ [![Model Context Protocol](https://img.shields.io/badge/MCP%20SDK-^1.13.0-green.svg)](https://modelcontextprotocol.io/)
5
+ [![Version](https://img.shields.io/badge/Version-1.2.2-blue.svg)](./CHANGELOG.md)
6
6
  [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
7
  [![Status](https://img.shields.io/badge/Status-Stable-green.svg)](https://github.com/cyanheads/pubmed-mcp-server/issues)
8
8
  [![GitHub](https://img.shields.io/github/stars/cyanheads/pubmed-mcp-server?style=social)](https://github.com/cyanheads/pubmed-mcp-server)
@@ -62,7 +62,7 @@ Leverages the robust utilities provided by the `mcp-ts-template`:
62
62
  - **Input Validation/Sanitization**: Uses `zod` for schema validation and custom sanitization logic.
63
63
  - **Request Context**: Tracking and correlation of operations via unique request IDs using `AsyncLocalStorage`.
64
64
  - **Type Safety**: Strong typing enforced by TypeScript and Zod schemas.
65
- - **HTTP Transport**: High-performance HTTP server using **Hono**, featuring session management with garbage collection, CORS, and IP-based rate limiting.
65
+ - **HTTP Transport**: High-performance HTTP server using **Hono**, featuring session management with garbage collection and CORS support.
66
66
  - **Authentication**: Robust authentication layer supporting JWT and OAuth 2.1, with fine-grained scope enforcement.
67
67
  - **Deployment**: Multi-stage `Dockerfile` for creating small, secure production images with native dependency support.
68
68
 
@@ -5,10 +5,10 @@
5
5
  * from the middleware layer down to the tool and resource handlers without
6
6
  * drilling props.
7
7
  *
8
- * @module src/mcp-server/transports/authentication/authContext
8
+ * @module src/mcp-server/transports/auth/core/authContext
9
9
  */
10
10
  import { AsyncLocalStorage } from "async_hooks";
11
- import type { AuthInfo } from "./types.js";
11
+ import type { AuthInfo } from "./authTypes.js";
12
12
  /**
13
13
  * Defines the structure of the store used within the AsyncLocalStorage.
14
14
  * It holds the authentication information for the current request context.
@@ -5,7 +5,7 @@
5
5
  * from the middleware layer down to the tool and resource handlers without
6
6
  * drilling props.
7
7
  *
8
- * @module src/mcp-server/transports/authentication/authContext
8
+ * @module src/mcp-server/transports/auth/core/authContext
9
9
  */
10
10
  import { AsyncLocalStorage } from "async_hooks";
11
11
  /**
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @fileoverview Shared types for authentication middleware.
3
- * @module src/mcp-server/transports/authentication/types
3
+ * @module src/mcp-server/transports/auth/core/auth.types
4
4
  */
5
5
  import type { AuthInfo as SdkAuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
6
6
  /**
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * @fileoverview Shared types for authentication middleware.
3
- * @module src/mcp-server/transports/authentication/types
3
+ * @module src/mcp-server/transports/auth/core/auth.types
4
4
  */
5
5
  export {};
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @fileoverview Provides utility functions for authorization, specifically for
3
3
  * checking token scopes against required permissions for a given operation.
4
- * @module src/mcp-server/transports/authentication/authUtils
4
+ * @module src/mcp-server/transports/auth/core/authUtils
5
5
  */
6
6
  /**
7
7
  * Checks if the current authentication context contains all the specified scopes.
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * @fileoverview Provides utility functions for authorization, specifically for
3
3
  * checking token scopes against required permissions for a given operation.
4
- * @module src/mcp-server/transports/authentication/authUtils
4
+ * @module src/mcp-server/transports/auth/core/authUtils
5
5
  */
6
- import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
7
- import { logger, requestContextService } from "../../../utils/index.js";
6
+ import { BaseErrorCode, McpError } from "../../../../types-global/errors.js";
7
+ import { logger, requestContextService } from "../../../../utils/index.js";
8
8
  import { authContext } from "./authContext.js";
9
9
  /**
10
10
  * Checks if the current authentication context contains all the specified scopes.
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @fileoverview Barrel file for the auth module.
3
+ * Exports core utilities and middleware strategies for easier imports.
4
+ * @module src/mcp-server/transports/auth/index
5
+ */
6
+ export { authContext } from "./core/authContext.js";
7
+ export { withRequiredScopes } from "./core/authUtils.js";
8
+ export type { AuthInfo } from "./core/authTypes.js";
9
+ export { mcpAuthMiddleware as jwtAuthMiddleware } from "./strategies/jwt/jwtMiddleware.js";
10
+ export { oauthMiddleware } from "./strategies/oauth/oauthMiddleware.js";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @fileoverview Barrel file for the auth module.
3
+ * Exports core utilities and middleware strategies for easier imports.
4
+ * @module src/mcp-server/transports/auth/index
5
+ */
6
+ export { authContext } from "./core/authContext.js";
7
+ export { withRequiredScopes } from "./core/authUtils.js";
8
+ export { mcpAuthMiddleware as jwtAuthMiddleware } from "./strategies/jwt/jwtMiddleware.js";
9
+ export { oauthMiddleware } from "./strategies/oauth/oauthMiddleware.js";
@@ -10,26 +10,18 @@
10
10
  * is attached to `c.env.incoming.auth`. This direct attachment to the raw Node.js
11
11
  * request object is for compatibility with the underlying SDK transport, which is
12
12
  * not Hono-context-aware.
13
- * If the token is missing, invalid, or expired, it returns an HTTP 401 Unauthorized response.
13
+ * If the token is missing, invalid, or expired, it throws an `McpError`, which is
14
+ * then handled by the centralized `httpErrorHandler`.
14
15
  *
15
16
  * @see {@link https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/basic/authorization.mdx | MCP Authorization Specification}
16
- * @module src/mcp-server/transports/authentication/authMiddleware
17
+ * @module src/mcp-server/transports/auth/strategies/jwt/jwtMiddleware
17
18
  */
18
19
  import { HttpBindings } from "@hono/node-server";
19
20
  import { Context, Next } from "hono";
20
- /**
21
- * Validates the presence of the MCP_AUTH_SECRET_KEY at startup.
22
- * This should be called once when the application is initializing.
23
- */
24
- export declare function initializeAuthMiddleware(): void;
25
21
  /**
26
22
  * Hono middleware for verifying JWT Bearer token authentication.
27
23
  * It attaches authentication info to `c.env.incoming.auth` for SDK compatibility with the node server.
28
24
  */
29
25
  export declare function mcpAuthMiddleware(c: Context<{
30
26
  Bindings: HttpBindings;
31
- }>, next: Next): Promise<void | (Response & import("hono").TypedResponse<{
32
- error: string;
33
- }, 500, "json">) | (Response & import("hono").TypedResponse<{
34
- error: string;
35
- }, 401, "json">)>;
27
+ }>, next: Next): Promise<void>;
@@ -10,32 +10,25 @@
10
10
  * is attached to `c.env.incoming.auth`. This direct attachment to the raw Node.js
11
11
  * request object is for compatibility with the underlying SDK transport, which is
12
12
  * not Hono-context-aware.
13
- * If the token is missing, invalid, or expired, it returns an HTTP 401 Unauthorized response.
13
+ * If the token is missing, invalid, or expired, it throws an `McpError`, which is
14
+ * then handled by the centralized `httpErrorHandler`.
14
15
  *
15
16
  * @see {@link https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/basic/authorization.mdx | MCP Authorization Specification}
16
- * @module src/mcp-server/transports/authentication/authMiddleware
17
+ * @module src/mcp-server/transports/auth/strategies/jwt/jwtMiddleware
17
18
  */
18
- import jwt from "jsonwebtoken";
19
- import { config, environment } from "../../../config/index.js";
20
- import { logger, requestContextService } from "../../../utils/index.js";
21
- import { authContext } from "./authContext.js";
22
- /**
23
- * Validates the presence of the MCP_AUTH_SECRET_KEY at startup.
24
- * This should be called once when the application is initializing.
25
- */
26
- export function initializeAuthMiddleware() {
27
- const context = requestContextService.createRequestContext({
28
- operation: "initializeAuthMiddleware",
29
- });
19
+ import { jwtVerify } from "jose";
20
+ import { config, environment } from "../../../../../config/index.js";
21
+ import { logger, requestContextService } from "../../../../../utils/index.js";
22
+ import { BaseErrorCode, McpError } from "../../../../../types-global/errors.js";
23
+ import { authContext } from "../../core/authContext.js";
24
+ // Startup Validation: Validate secret key presence on module load.
25
+ if (config.mcpAuthMode === "jwt") {
30
26
  if (environment === "production" && !config.mcpAuthSecretKey) {
31
- logger.fatal("CRITICAL: MCP_AUTH_SECRET_KEY is not set in production environment. Authentication cannot proceed securely.", context);
27
+ logger.fatal("CRITICAL: MCP_AUTH_SECRET_KEY is not set in production environment for JWT auth. Authentication cannot proceed securely.");
32
28
  throw new Error("MCP_AUTH_SECRET_KEY must be set in production environment for JWT authentication.");
33
29
  }
34
30
  else if (!config.mcpAuthSecretKey) {
35
- logger.warning("MCP_AUTH_SECRET_KEY is not set. Authentication middleware will bypass checks (DEVELOPMENT ONLY). This is insecure for production.", context);
36
- }
37
- else {
38
- logger.debug("Auth middleware secret key check passed.", context);
31
+ logger.warning("MCP_AUTH_SECRET_KEY is not set. JWT auth middleware will bypass checks (DEVELOPMENT ONLY). This is insecure for production.");
39
32
  }
40
33
  }
41
34
  /**
@@ -50,6 +43,10 @@ export async function mcpAuthMiddleware(c, next) {
50
43
  });
51
44
  logger.debug("Running MCP Authentication Middleware (Bearer Token Validation)...", context);
52
45
  const reqWithAuth = c.env.incoming;
46
+ // If JWT auth is not enabled, skip the middleware.
47
+ if (config.mcpAuthMode !== "jwt") {
48
+ return await next();
49
+ }
53
50
  // Development Mode Bypass
54
51
  if (!config.mcpAuthSecretKey) {
55
52
  if (environment !== "production") {
@@ -68,28 +65,23 @@ export async function mcpAuthMiddleware(c, next) {
68
65
  }
69
66
  else {
70
67
  logger.error("FATAL: MCP_AUTH_SECRET_KEY is missing in production. Cannot bypass auth.", context);
71
- return c.json({ error: "Server configuration error: Authentication key missing." }, 500);
68
+ throw new McpError(BaseErrorCode.INTERNAL_ERROR, "Server configuration error: Authentication key missing.");
72
69
  }
73
70
  }
71
+ const secretKey = new TextEncoder().encode(config.mcpAuthSecretKey);
74
72
  const authHeader = c.req.header("Authorization");
75
73
  if (!authHeader || !authHeader.startsWith("Bearer ")) {
76
74
  logger.warning("Authentication failed: Missing or malformed Authorization header (Bearer scheme required).", context);
77
- return c.json({
78
- error: "Unauthorized: Missing or invalid authentication token format.",
79
- }, 401);
75
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Missing or invalid authentication token format.");
80
76
  }
81
77
  const tokenParts = authHeader.split(" ");
82
78
  if (tokenParts.length !== 2 || tokenParts[0] !== "Bearer" || !tokenParts[1]) {
83
79
  logger.warning("Authentication failed: Malformed Bearer token.", context);
84
- return c.json({ error: "Unauthorized: Malformed authentication token." }, 401);
80
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Malformed authentication token.");
85
81
  }
86
82
  const rawToken = tokenParts[1];
87
83
  try {
88
- const decoded = jwt.verify(rawToken, config.mcpAuthSecretKey);
89
- if (typeof decoded === "string") {
90
- logger.warning("Authentication failed: JWT decoded to a string, expected an object payload.", context);
91
- return c.json({ error: "Unauthorized: Invalid token payload format." }, 401);
92
- }
84
+ const { payload: decoded } = await jwtVerify(rawToken, secretKey);
93
85
  const clientIdFromToken = typeof decoded.cid === "string"
94
86
  ? decoded.cid
95
87
  : typeof decoded.client_id === "string"
@@ -97,7 +89,7 @@ export async function mcpAuthMiddleware(c, next) {
97
89
  : undefined;
98
90
  if (!clientIdFromToken) {
99
91
  logger.warning("Authentication failed: JWT 'cid' or 'client_id' claim is missing or not a string.", { ...context, jwtPayloadKeys: Object.keys(decoded) });
100
- return c.json({ error: "Unauthorized: Invalid token, missing client identifier." }, 401);
92
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Invalid token, missing client identifier.");
101
93
  }
102
94
  let scopesFromToken = [];
103
95
  if (Array.isArray(decoded.scp) &&
@@ -113,7 +105,7 @@ export async function mcpAuthMiddleware(c, next) {
113
105
  }
114
106
  if (scopesFromToken.length === 0) {
115
107
  logger.warning("Authentication failed: Token resulted in an empty scope array, and scopes are required.", { ...context, jwtPayloadKeys: Object.keys(decoded) });
116
- return c.json({ error: "Unauthorized: Token must contain valid, non-empty scopes." }, 401);
108
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Token must contain valid, non-empty scopes.");
117
109
  }
118
110
  reqWithAuth.auth = {
119
111
  token: rawToken,
@@ -131,26 +123,27 @@ export async function mcpAuthMiddleware(c, next) {
131
123
  await authContext.run({ authInfo }, next);
132
124
  }
133
125
  catch (error) {
134
- let errorMessage = "Invalid token";
135
- if (error instanceof jwt.TokenExpiredError) {
136
- errorMessage = "Token expired";
126
+ let errorMessage = "Invalid token.";
127
+ let errorCode = BaseErrorCode.UNAUTHORIZED;
128
+ if (error instanceof Error && error.name === "JWTExpired") {
129
+ errorMessage = "Token expired.";
137
130
  logger.warning("Authentication failed: Token expired.", {
138
131
  ...context,
139
- expiredAt: error.expiredAt,
132
+ errorName: error.name,
140
133
  });
141
134
  }
142
- else if (error instanceof jwt.JsonWebTokenError) {
143
- errorMessage = `Invalid token: ${error.message}`;
144
- logger.warning(`Authentication failed: ${errorMessage}`, { ...context });
145
- }
146
135
  else if (error instanceof Error) {
147
- errorMessage = `Verification error: ${error.message}`;
148
- logger.error("Authentication failed: Unexpected error during token verification.", { ...context, error: error.message });
136
+ errorMessage = `Invalid token: ${error.message}`;
137
+ logger.warning(`Authentication failed: ${errorMessage}`, {
138
+ ...context,
139
+ errorName: error.name,
140
+ });
149
141
  }
150
142
  else {
151
- errorMessage = "Unknown verification error";
143
+ errorMessage = "Unknown verification error.";
144
+ errorCode = BaseErrorCode.INTERNAL_ERROR;
152
145
  logger.error("Authentication failed: Unexpected non-error exception during token verification.", { ...context, error });
153
146
  }
154
- return c.json({ error: `Unauthorized: ${errorMessage}.` }, 401);
147
+ throw new McpError(errorCode, errorMessage);
155
148
  }
156
149
  }
@@ -5,7 +5,7 @@
5
5
  * On success, it populates an AuthInfo object and stores it in an AsyncLocalStorage
6
6
  * context for use in downstream handlers.
7
7
  *
8
- * @module src/mcp-server/transports/authentication/oauthMiddleware
8
+ * @module src/mcp-server/transports/auth/strategies/oauth/oauthMiddleware
9
9
  */
10
10
  import { HttpBindings } from "@hono/node-server";
11
11
  import { Context, Next } from "hono";
@@ -17,8 +17,4 @@ import { Context, Next } from "hono";
17
17
  */
18
18
  export declare function oauthMiddleware(c: Context<{
19
19
  Bindings: HttpBindings;
20
- }>, next: Next): Promise<(Response & import("hono").TypedResponse<{
21
- error: string;
22
- }, 500, "json">) | (Response & import("hono").TypedResponse<{
23
- error: string;
24
- }, 401, "json">) | undefined>;
20
+ }>, next: Next): Promise<void>;
@@ -5,14 +5,14 @@
5
5
  * On success, it populates an AuthInfo object and stores it in an AsyncLocalStorage
6
6
  * context for use in downstream handlers.
7
7
  *
8
- * @module src/mcp-server/transports/authentication/oauthMiddleware
8
+ * @module src/mcp-server/transports/auth/strategies/oauth/oauthMiddleware
9
9
  */
10
10
  import { createRemoteJWKSet, jwtVerify } from "jose";
11
- import { config } from "../../../config/index.js";
12
- import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
13
- import { ErrorHandler } from "../../../utils/internal/errorHandler.js";
14
- import { logger, requestContextService } from "../../../utils/index.js";
15
- import { authContext } from "./authContext.js";
11
+ import { config } from "../../../../../config/index.js";
12
+ import { BaseErrorCode, McpError } from "../../../../../types-global/errors.js";
13
+ import { logger, requestContextService } from "../../../../../utils/index.js";
14
+ import { ErrorHandler } from "../../../../../utils/internal/errorHandler.js";
15
+ import { authContext } from "../../core/authContext.js";
16
16
  // --- Startup Validation ---
17
17
  // Ensures that necessary OAuth configuration is present when the mode is 'oauth'.
18
18
  if (config.mcpAuthMode === "oauth") {
@@ -57,6 +57,10 @@ if (config.mcpAuthMode === "oauth" && config.oauthIssuerUrl) {
57
57
  * @param next - The function to call to proceed to the next middleware.
58
58
  */
59
59
  export async function oauthMiddleware(c, next) {
60
+ // If OAuth is not the configured auth mode, skip this middleware.
61
+ if (config.mcpAuthMode !== "oauth") {
62
+ return await next();
63
+ }
60
64
  const context = requestContextService.createRequestContext({
61
65
  operation: "oauthMiddleware",
62
66
  httpMethod: c.req.method,
@@ -64,13 +68,12 @@ export async function oauthMiddleware(c, next) {
64
68
  });
65
69
  if (!jwks) {
66
70
  // This should not happen if startup validation is correct, but it's a safeguard.
67
- const error = new McpError(BaseErrorCode.CONFIGURATION_ERROR, "OAuth middleware is active, but JWKS client is not initialized.", context);
68
- ErrorHandler.handleError(error, { operation: "oauthMiddleware", context });
69
- return c.json({ error: "Server configuration error." }, 500);
71
+ // This should not happen if startup validation is correct, but it's a safeguard.
72
+ throw new McpError(BaseErrorCode.CONFIGURATION_ERROR, "OAuth middleware is active, but JWKS client is not initialized.", context);
70
73
  }
71
74
  const authHeader = c.req.header("Authorization");
72
75
  if (!authHeader || !authHeader.startsWith("Bearer ")) {
73
- return c.json({ error: "Unauthorized: Missing or invalid token format." }, 401);
76
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Missing or invalid token format.");
74
77
  }
75
78
  const token = authHeader.substring(7);
76
79
  try {
@@ -80,10 +83,14 @@ export async function oauthMiddleware(c, next) {
80
83
  });
81
84
  // The 'scope' claim is typically a space-delimited string in OAuth 2.1.
82
85
  const scopes = typeof payload.scope === "string" ? payload.scope.split(" ") : [];
86
+ if (scopes.length === 0) {
87
+ logger.warning("Authentication failed: Token contains no scopes, but scopes are required.", { ...context, jwtPayloadKeys: Object.keys(payload) });
88
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Token must contain valid, non-empty scopes.");
89
+ }
83
90
  const clientId = typeof payload.client_id === "string" ? payload.client_id : undefined;
84
91
  if (!clientId) {
85
92
  logger.warning("Authentication failed: OAuth token 'client_id' claim is missing or not a string.", { ...context, jwtPayloadKeys: Object.keys(payload) });
86
- return c.json({ error: "Unauthorized: Invalid token, missing client identifier." }, 401);
93
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Invalid token, missing client identifier.");
87
94
  }
88
95
  const authInfo = {
89
96
  token,
@@ -97,13 +104,21 @@ export async function oauthMiddleware(c, next) {
97
104
  await authContext.run({ authInfo }, next);
98
105
  }
99
106
  catch (error) {
100
- logger.warning("OAuth token validation failed", {
101
- ...context,
102
- errorName: error.name,
103
- errorMessage: error.message,
107
+ if (error instanceof Error && error.name === "JWTExpired") {
108
+ logger.warning("Authentication failed: OAuth token expired.", context);
109
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, "Token expired.");
110
+ }
111
+ const handledError = ErrorHandler.handleError(error, {
112
+ operation: "oauthMiddleware",
113
+ context,
114
+ rethrow: false, // We will throw a new McpError below
104
115
  });
105
- // The `jose` library provides specific error codes like 'ERR_JWT_EXPIRED' or 'ERR_JWS_INVALID'
106
- const message = `Unauthorized: ${error.message || "Invalid token"}`;
107
- return c.json({ error: message }, 401);
116
+ // Ensure we always throw an McpError for consistency
117
+ if (handledError instanceof McpError) {
118
+ throw handledError;
119
+ }
120
+ else {
121
+ throw new McpError(BaseErrorCode.UNAUTHORIZED, `Unauthorized: ${handledError.message || "Invalid token"}`, { originalError: handledError.name });
122
+ }
108
123
  }
109
124
  }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @fileoverview Centralized error handler for the Hono HTTP transport.
3
+ * This middleware intercepts errors that occur during request processing,
4
+ * standardizes them using the application's ErrorHandler utility, and
5
+ * formats them into a consistent JSON-RPC error response.
6
+ * @module src/mcp-server/transports/httpErrorHandler
7
+ */
8
+ import { Context } from "hono";
9
+ import { BaseErrorCode } from "../../types-global/errors.js";
10
+ /**
11
+ * A centralized error handling middleware for Hono.
12
+ * This function is registered with `app.onError()` and will catch any errors
13
+ * thrown from preceding middleware or route handlers.
14
+ *
15
+ * @param err - The error that was thrown.
16
+ * @param c - The Hono context object for the request.
17
+ * @returns A Response object containing the formatted JSON-RPC error.
18
+ */
19
+ export declare const httpErrorHandler: (err: Error, c: Context) => Promise<Response & import("hono").TypedResponse<{
20
+ jsonrpc: string;
21
+ error: {
22
+ code: number | BaseErrorCode;
23
+ message: string;
24
+ };
25
+ id: string | number | null;
26
+ }, import("hono/utils/http-status").ContentfulStatusCode, "json">>;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @fileoverview Centralized error handler for the Hono HTTP transport.
3
+ * This middleware intercepts errors that occur during request processing,
4
+ * standardizes them using the application's ErrorHandler utility, and
5
+ * formats them into a consistent JSON-RPC error response.
6
+ * @module src/mcp-server/transports/httpErrorHandler
7
+ */
8
+ import { BaseErrorCode, McpError } from "../../types-global/errors.js";
9
+ import { ErrorHandler, requestContextService } from "../../utils/index.js";
10
+ /**
11
+ * A centralized error handling middleware for Hono.
12
+ * This function is registered with `app.onError()` and will catch any errors
13
+ * thrown from preceding middleware or route handlers.
14
+ *
15
+ * @param err - The error that was thrown.
16
+ * @param c - The Hono context object for the request.
17
+ * @returns A Response object containing the formatted JSON-RPC error.
18
+ */
19
+ export const httpErrorHandler = async (err, c) => {
20
+ const context = requestContextService.createRequestContext({
21
+ operation: "httpErrorHandler",
22
+ path: c.req.path,
23
+ method: c.req.method,
24
+ });
25
+ const handledError = ErrorHandler.handleError(err, {
26
+ operation: "httpTransport",
27
+ context,
28
+ });
29
+ let status = 500;
30
+ if (handledError instanceof McpError) {
31
+ switch (handledError.code) {
32
+ case BaseErrorCode.NOT_FOUND:
33
+ status = 404;
34
+ break;
35
+ case BaseErrorCode.UNAUTHORIZED:
36
+ status = 401;
37
+ break;
38
+ case BaseErrorCode.FORBIDDEN:
39
+ status = 403;
40
+ break;
41
+ case BaseErrorCode.VALIDATION_ERROR:
42
+ status = 400;
43
+ break;
44
+ case BaseErrorCode.CONFLICT:
45
+ status = 409;
46
+ break;
47
+ case BaseErrorCode.RATE_LIMITED:
48
+ status = 429;
49
+ break;
50
+ default:
51
+ status = 500;
52
+ }
53
+ }
54
+ // Attempt to get the request ID from the body, but don't fail if it's not there or unreadable.
55
+ let requestId = null;
56
+ try {
57
+ const body = await c.req.json();
58
+ requestId = body?.id || null;
59
+ }
60
+ catch {
61
+ // Ignore parsing errors, requestId will remain null
62
+ }
63
+ const errorCode = handledError instanceof McpError ? handledError.code : -32603;
64
+ c.status(status);
65
+ return c.json({
66
+ jsonrpc: "2.0",
67
+ error: {
68
+ code: errorCode,
69
+ message: handledError.message,
70
+ },
71
+ id: requestId,
72
+ });
73
+ };
@@ -1,10 +1,15 @@
1
1
  /**
2
- * @fileoverview Handles the setup and management of the Streamable HTTP MCP transport using Hono.
3
- * Implements the MCP Specification 2025-03-26 for Streamable HTTP.
4
- * This includes creating a Hono server, configuring middleware (CORS, Authentication),
5
- * defining request routing for the single MCP endpoint (POST/GET/DELETE),
6
- * managing server-side sessions, handling Server-Sent Events (SSE) for streaming,
7
- * and binding to a network port with retry logic for port conflicts.
2
+ * @fileoverview Configures and starts the Streamable HTTP MCP transport using Hono.
3
+ * This module integrates the `@modelcontextprotocol/sdk`'s `StreamableHTTPServerTransport`
4
+ * into a Hono web server. Its responsibilities include:
5
+ * - Creating a Hono server instance.
6
+ * - Applying and configuring middleware for CORS, rate limiting, and authentication (JWT/OAuth).
7
+ * - Defining the routes (`/mcp` endpoint for POST, GET, DELETE) to handle the MCP lifecycle.
8
+ * - Orchestrating session management by mapping session IDs to SDK transport instances.
9
+ * - Implementing port-binding logic with automatic retry on conflicts.
10
+ *
11
+ * The underlying implementation of the MCP Streamable HTTP specification, including
12
+ * Server-Sent Events (SSE) for streaming, is handled by the SDK's transport class.
8
13
  *
9
14
  * Specification Reference:
10
15
  * https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/basic/transports.mdx#streamable-http
@@ -1,10 +1,15 @@
1
1
  /**
2
- * @fileoverview Handles the setup and management of the Streamable HTTP MCP transport using Hono.
3
- * Implements the MCP Specification 2025-03-26 for Streamable HTTP.
4
- * This includes creating a Hono server, configuring middleware (CORS, Authentication),
5
- * defining request routing for the single MCP endpoint (POST/GET/DELETE),
6
- * managing server-side sessions, handling Server-Sent Events (SSE) for streaming,
7
- * and binding to a network port with retry logic for port conflicts.
2
+ * @fileoverview Configures and starts the Streamable HTTP MCP transport using Hono.
3
+ * This module integrates the `@modelcontextprotocol/sdk`'s `StreamableHTTPServerTransport`
4
+ * into a Hono web server. Its responsibilities include:
5
+ * - Creating a Hono server instance.
6
+ * - Applying and configuring middleware for CORS, rate limiting, and authentication (JWT/OAuth).
7
+ * - Defining the routes (`/mcp` endpoint for POST, GET, DELETE) to handle the MCP lifecycle.
8
+ * - Orchestrating session management by mapping session IDs to SDK transport instances.
9
+ * - Implementing port-binding logic with automatic retry on conflicts.
10
+ *
11
+ * The underlying implementation of the MCP Streamable HTTP specification, including
12
+ * Server-Sent Events (SSE) for streaming, is handled by the SDK's transport class.
8
13
  *
9
14
  * Specification Reference:
10
15
  * https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/basic/transports.mdx#streamable-http
@@ -19,17 +24,19 @@ import http from "http";
19
24
  import { randomUUID } from "node:crypto";
20
25
  import { config } from "../../config/index.js";
21
26
  import { BaseErrorCode, McpError } from "../../types-global/errors.js";
22
- import { ErrorHandler, logger, rateLimiter, requestContextService, } from "../../utils/index.js";
23
- import { initializeAuthMiddleware, mcpAuthMiddleware, } from "./authentication/authMiddleware.js";
24
- import { oauthMiddleware } from "./authentication/oauthMiddleware.js";
27
+ import { logger, rateLimiter, requestContextService, } from "../../utils/index.js";
28
+ import { jwtAuthMiddleware, oauthMiddleware, } from "./auth/index.js";
29
+ import { httpErrorHandler } from "./httpErrorHandler.js";
25
30
  const HTTP_PORT = config.mcpHttpPort;
26
31
  const HTTP_HOST = config.mcpHttpHost;
27
32
  const MCP_ENDPOINT_PATH = "/mcp";
28
33
  const MAX_PORT_RETRIES = 15;
29
- const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
30
- const SESSION_GC_INTERVAL_MS = 60 * 1000;
31
- const httpTransports = {};
32
- const sessionActivity = {};
34
+ // The transports map will store active sessions, keyed by session ID.
35
+ // NOTE: This is an in-memory session store, which is a known limitation for scalability.
36
+ // It will not work in a multi-process (clustered) or serverless environment.
37
+ // For a scalable deployment, this would need to be replaced with a distributed
38
+ // store like Redis or Memcached.
39
+ const transports = {};
33
40
  async function isPortInUse(port, host, parentContext) {
34
41
  const checkContext = requestContextService.createRequestContext({
35
42
  ...parentContext,
@@ -57,7 +64,11 @@ function startHttpServerWithRetry(app, initialPort, host, maxRetries, parentCont
57
64
  return new Promise(async (resolve, reject) => {
58
65
  for (let i = 0; i <= maxRetries; i++) {
59
66
  const currentPort = initialPort + i;
60
- const attemptContext = { ...startContext, port: currentPort, attempt: i + 1 };
67
+ const attemptContext = {
68
+ ...startContext,
69
+ port: currentPort,
70
+ attempt: i + 1,
71
+ };
61
72
  if (await isPortInUse(currentPort, host, attemptContext)) {
62
73
  logger.warning(`Port ${currentPort} is in use, retrying...`, attemptContext);
63
74
  continue;
@@ -65,7 +76,10 @@ function startHttpServerWithRetry(app, initialPort, host, maxRetries, parentCont
65
76
  try {
66
77
  const serverInstance = serve({ fetch: app.fetch, port: currentPort, hostname: host }, (info) => {
67
78
  const serverAddress = `http://${info.address}:${info.port}${MCP_ENDPOINT_PATH}`;
68
- logger.info(`HTTP transport listening at ${serverAddress}`, { ...attemptContext, address: serverAddress });
79
+ logger.info(`HTTP transport listening at ${serverAddress}`, {
80
+ ...attemptContext,
81
+ address: serverAddress,
82
+ });
69
83
  if (process.stdout.isTTY) {
70
84
  console.log(`\n🚀 MCP Server running at: ${serverAddress}\n`);
71
85
  }
@@ -84,27 +98,20 @@ function startHttpServerWithRetry(app, initialPort, host, maxRetries, parentCont
84
98
  });
85
99
  }
86
100
  export async function startHttpTransport(createServerInstanceFn, parentContext) {
87
- initializeAuthMiddleware();
88
101
  const app = new Hono();
89
102
  const transportContext = requestContextService.createRequestContext({
90
103
  ...parentContext,
91
104
  component: "HttpTransportSetup",
92
105
  });
93
- setInterval(() => {
94
- const now = Date.now();
95
- const gcContext = requestContextService.createRequestContext({ operation: "SessionGarbageCollector" });
96
- for (const sessionId in sessionActivity) {
97
- if (now - sessionActivity[sessionId] > SESSION_TIMEOUT_MS) {
98
- logger.info(`Session ${sessionId} timed out. Cleaning up.`, { ...gcContext, sessionId });
99
- httpTransports[sessionId]?.close();
100
- delete sessionActivity[sessionId];
101
- }
102
- }
103
- }, SESSION_GC_INTERVAL_MS);
104
106
  app.use("*", cors({
105
107
  origin: config.mcpAllowedOrigins || [],
106
108
  allowMethods: ["GET", "POST", "DELETE", "OPTIONS"],
107
- allowHeaders: ["Content-Type", "Mcp-Session-Id", "Last-Event-ID", "Authorization"],
109
+ allowHeaders: [
110
+ "Content-Type",
111
+ "Mcp-Session-Id",
112
+ "Last-Event-ID",
113
+ "Authorization",
114
+ ],
108
115
  credentials: true,
109
116
  }));
110
117
  app.use("*", async (c, next) => {
@@ -112,98 +119,90 @@ export async function startHttpTransport(createServerInstanceFn, parentContext)
112
119
  await next();
113
120
  });
114
121
  app.use(MCP_ENDPOINT_PATH, async (c, next) => {
122
+ // NOTE (Security): The 'x-forwarded-for' header is used for rate limiting.
123
+ // This is only secure if the server is run behind a trusted proxy that
124
+ // correctly sets or validates this header.
115
125
  const clientIp = c.req.header("x-forwarded-for")?.split(",")[0].trim() || "unknown_ip";
116
- const context = requestContextService.createRequestContext({ operation: "httpRateLimitCheck", ipAddress: clientIp });
117
- try {
118
- rateLimiter.check(clientIp, context);
119
- await next();
120
- }
121
- catch (error) {
122
- const handledError = ErrorHandler.handleError(error, { operation: "rateLimitMiddleware", context });
123
- return c.json({
124
- jsonrpc: "2.0",
125
- error: { code: -32000, message: handledError.message },
126
- id: (await c.req.json().catch(() => ({})))?.id || null,
127
- }, 429);
128
- }
126
+ const context = requestContextService.createRequestContext({
127
+ operation: "httpRateLimitCheck",
128
+ ipAddress: clientIp,
129
+ });
130
+ // Let the centralized error handler catch rate limit errors
131
+ rateLimiter.check(clientIp, context);
132
+ await next();
129
133
  });
130
134
  if (config.mcpAuthMode === "oauth") {
131
135
  app.use(MCP_ENDPOINT_PATH, oauthMiddleware);
132
136
  }
133
137
  else {
134
- app.use(MCP_ENDPOINT_PATH, mcpAuthMiddleware);
138
+ app.use(MCP_ENDPOINT_PATH, jwtAuthMiddleware);
135
139
  }
140
+ // Centralized Error Handling
141
+ app.onError(httpErrorHandler);
136
142
  app.post(MCP_ENDPOINT_PATH, async (c) => {
137
- const postContext = requestContextService.createRequestContext({ ...transportContext, operation: "handlePost" });
138
- let transport;
139
- try {
140
- const body = await c.req.json();
141
- const sessionId = c.req.header("mcp-session-id");
142
- transport = sessionId ? httpTransports[sessionId] : undefined;
143
- if (transport && sessionId)
144
- sessionActivity[sessionId] = Date.now();
145
- if (isInitializeRequest(body)) {
146
- if (transport) {
147
- logger.warning("Re-initializing existing session.", { ...postContext, sessionId });
148
- await transport.close();
149
- }
150
- transport = new StreamableHTTPServerTransport({
151
- sessionIdGenerator: () => randomUUID(),
152
- onsessioninitialized: (newId) => {
153
- httpTransports[newId] = transport;
154
- sessionActivity[newId] = Date.now();
155
- logger.info(`HTTP Session created: ${newId}`, { ...postContext, newSessionId: newId });
156
- },
143
+ const postContext = requestContextService.createRequestContext({
144
+ ...transportContext,
145
+ operation: "handlePost",
146
+ });
147
+ const body = await c.req.json();
148
+ const sessionId = c.req.header("mcp-session-id");
149
+ let transport = sessionId
150
+ ? transports[sessionId]
151
+ : undefined;
152
+ if (isInitializeRequest(body)) {
153
+ // If a transport already exists for a session, it's a re-initialization.
154
+ if (transport) {
155
+ logger.warning("Re-initializing existing session.", {
156
+ ...postContext,
157
+ sessionId,
157
158
  });
158
- transport.onclose = () => {
159
- const closedSessionId = transport.sessionId;
160
- if (closedSessionId) {
161
- delete httpTransports[closedSessionId];
162
- delete sessionActivity[closedSessionId];
163
- logger.info(`HTTP Session closed: ${closedSessionId}`, { ...postContext, closedSessionId });
164
- }
165
- };
166
- const server = await createServerInstanceFn();
167
- await server.connect(transport);
168
- }
169
- else if (!transport) {
170
- throw new McpError(BaseErrorCode.NOT_FOUND, "Invalid or expired session ID.");
159
+ await transport.close(); // This will trigger the onclose handler.
171
160
  }
172
- return await transport.handleRequest(c.env.incoming, c.env.outgoing, body);
161
+ // Create a new transport for a new session.
162
+ const newTransport = new StreamableHTTPServerTransport({
163
+ sessionIdGenerator: () => randomUUID(),
164
+ onsessioninitialized: (newId) => {
165
+ transports[newId] = newTransport;
166
+ logger.info(`HTTP Session created: ${newId}`, {
167
+ ...postContext,
168
+ newSessionId: newId,
169
+ });
170
+ },
171
+ });
172
+ // Set up cleanup logic for when the transport is closed.
173
+ newTransport.onclose = () => {
174
+ const closedSessionId = newTransport.sessionId;
175
+ if (closedSessionId && transports[closedSessionId]) {
176
+ delete transports[closedSessionId];
177
+ logger.info(`HTTP Session closed: ${closedSessionId}`, {
178
+ ...postContext,
179
+ closedSessionId,
180
+ });
181
+ }
182
+ };
183
+ // Connect the new transport to a new server instance.
184
+ const server = await createServerInstanceFn();
185
+ await server.connect(newTransport);
186
+ transport = newTransport;
173
187
  }
174
- catch (err) {
175
- const handledError = ErrorHandler.handleError(err, { operation: "handlePost", context: postContext });
176
- const requestId = (await c.req.json().catch(() => ({})))?.id || null;
177
- return c.json({
178
- jsonrpc: "2.0",
179
- error: { code: -32603, message: handledError.message },
180
- id: requestId,
181
- }, handledError instanceof McpError && handledError.code === BaseErrorCode.NOT_FOUND ? 404 : 500);
188
+ else if (!transport) {
189
+ // If it's not an initialization request and no transport was found, it's an error.
190
+ throw new McpError(BaseErrorCode.NOT_FOUND, "Invalid or expired session ID.");
182
191
  }
192
+ // Pass the request to the transport to handle.
193
+ return await transport.handleRequest(c.env.incoming, c.env.outgoing, body);
183
194
  });
184
- const handleSessionReq = async (c) => {
185
- const method = c.req.method;
186
- const sessionReqContext = requestContextService.createRequestContext({ ...transportContext, operation: `handle${method}` });
187
- try {
188
- const sessionId = c.req.header("mcp-session-id");
189
- const transport = sessionId ? httpTransports[sessionId] : undefined;
190
- if (!transport) {
191
- throw new McpError(BaseErrorCode.NOT_FOUND, "Session not found or expired.");
192
- }
193
- if (sessionId)
194
- sessionActivity[sessionId] = Date.now();
195
- return await transport.handleRequest(c.env.incoming, c.env.outgoing);
196
- }
197
- catch (err) {
198
- const handledError = ErrorHandler.handleError(err, { operation: `handle${method}`, context: sessionReqContext });
199
- return c.json({
200
- jsonrpc: "2.0",
201
- error: { code: -32603, message: handledError.message },
202
- id: null,
203
- }, handledError instanceof McpError && handledError.code === BaseErrorCode.NOT_FOUND ? 404 : 500);
195
+ // A reusable handler for GET and DELETE requests which operate on existing sessions.
196
+ const handleSessionRequest = async (c) => {
197
+ const sessionId = c.req.header("mcp-session-id");
198
+ const transport = sessionId ? transports[sessionId] : undefined;
199
+ if (!transport) {
200
+ throw new McpError(BaseErrorCode.NOT_FOUND, "Session not found or expired.");
204
201
  }
202
+ // Let the transport handle the streaming (GET) or termination (DELETE) request.
203
+ return await transport.handleRequest(c.env.incoming, c.env.outgoing);
205
204
  };
206
- app.get(MCP_ENDPOINT_PATH, handleSessionReq);
207
- app.delete(MCP_ENDPOINT_PATH, handleSessionReq);
205
+ app.get(MCP_ENDPOINT_PATH, handleSessionRequest);
206
+ app.delete(MCP_ENDPOINT_PATH, handleSessionRequest);
208
207
  return startHttpServerWithRetry(app, HTTP_PORT, HTTP_HOST, MAX_PORT_RETRIES, transportContext);
209
208
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyanheads/pubmed-mcp-server",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "A Model Context Protocol (MCP) server enabling AI agents to intelligently search, retrieve, and analyze biomedical literature from PubMed via NCBI E-utilities. Built on the mcp-ts-template for robust, production-ready performance.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -33,19 +33,19 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@hono/node-server": "^1.14.4",
36
- "@modelcontextprotocol/sdk": "^1.12.3",
37
- "@types/jsonwebtoken": "^9.0.9",
38
- "@types/node": "^24.0.1",
36
+ "@modelcontextprotocol/sdk": "^1.13.0",
37
+ "@types/jsonwebtoken": "^9.0.10",
38
+ "@types/node": "^24.0.3",
39
39
  "@types/sanitize-html": "^2.16.0",
40
- "@types/validator": "13.15.1",
40
+ "@types/validator": "13.15.2",
41
41
  "axios": "^1.10.0",
42
42
  "chrono-node": "^2.8.3",
43
43
  "dotenv": "^16.5.0",
44
44
  "fast-xml-parser": "^5.2.5",
45
- "hono": "^4.7.11",
45
+ "hono": "^4.8.2",
46
46
  "jose": "^6.0.11",
47
47
  "jsonwebtoken": "^9.0.2",
48
- "openai": "^5.3.0",
48
+ "openai": "^5.6.0",
49
49
  "partial-json": "^0.1.7",
50
50
  "sanitize-html": "^2.17.0",
51
51
  "tiktoken": "^1.0.21",
@@ -56,7 +56,7 @@
56
56
  "chartjs-node-canvas": "^5.0.0",
57
57
  "winston": "^3.17.0",
58
58
  "winston-transport": "^4.9.0",
59
- "zod": "^3.25.64"
59
+ "zod": "^3.25.67"
60
60
  },
61
61
  "keywords": [
62
62
  "mcp",