mcp-ts-template 1.2.1 → 1.2.3
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 +2 -2
- package/dist/config/index.d.ts +33 -117
- package/dist/config/index.js +98 -275
- package/dist/index.js +34 -120
- package/dist/mcp-client/client.d.ts +78 -16
- package/dist/mcp-client/client.js +214 -172
- package/dist/mcp-client/configLoader.d.ts +5 -17
- package/dist/mcp-client/configLoader.js +7 -11
- package/dist/mcp-client/index.d.ts +2 -1
- package/dist/mcp-client/index.js +2 -1
- package/dist/mcp-client/transport.d.ts +7 -15
- package/dist/mcp-client/transport.js +7 -9
- package/dist/mcp-server/resources/echoResource/echoResourceLogic.d.ts +20 -45
- package/dist/mcp-server/resources/echoResource/echoResourceLogic.js +27 -56
- package/dist/mcp-server/resources/echoResource/index.d.ts +1 -1
- package/dist/mcp-server/resources/echoResource/index.js +1 -1
- package/dist/mcp-server/resources/echoResource/registration.d.ts +12 -14
- package/dist/mcp-server/resources/echoResource/registration.js +52 -72
- package/dist/mcp-server/server.d.ts +6 -12
- package/dist/mcp-server/server.js +32 -62
- package/dist/mcp-server/tools/echoTool/echoToolLogic.d.ts +14 -24
- package/dist/mcp-server/tools/echoTool/echoToolLogic.js +15 -35
- package/dist/mcp-server/tools/echoTool/index.d.ts +1 -1
- package/dist/mcp-server/tools/echoTool/index.js +1 -1
- package/dist/mcp-server/tools/echoTool/registration.d.ts +8 -11
- package/dist/mcp-server/tools/echoTool/registration.js +33 -63
- package/dist/mcp-server/transports/authentication/authMiddleware.d.ts +7 -40
- package/dist/mcp-server/transports/authentication/authMiddleware.js +83 -51
- package/dist/mcp-server/transports/httpTransport.d.ts +5 -17
- package/dist/mcp-server/transports/httpTransport.js +25 -43
- package/dist/mcp-server/transports/stdioTransport.d.ts +10 -11
- package/dist/mcp-server/transports/stdioTransport.js +11 -13
- package/dist/services/openRouterProvider.d.ts +28 -53
- package/dist/services/openRouterProvider.js +51 -53
- package/dist/types-global/errors.d.ts +4 -36
- package/dist/types-global/errors.js +5 -29
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +2 -2
- package/dist/utils/internal/errorHandler.d.ts +34 -69
- package/dist/utils/internal/errorHandler.js +42 -73
- package/dist/utils/internal/index.d.ts +1 -1
- package/dist/utils/internal/index.js +1 -1
- package/dist/utils/internal/logger.d.ts +39 -111
- package/dist/utils/internal/logger.js +54 -121
- package/dist/utils/internal/requestContext.d.ts +7 -21
- package/dist/utils/internal/requestContext.js +10 -17
- package/dist/utils/metrics/index.d.ts +1 -1
- package/dist/utils/metrics/index.js +1 -1
- package/dist/utils/metrics/tokenCounter.d.ts +15 -23
- package/dist/utils/metrics/tokenCounter.js +20 -29
- package/dist/utils/parsing/dateParser.d.ts +32 -20
- package/dist/utils/parsing/dateParser.js +33 -21
- package/dist/utils/parsing/index.d.ts +2 -2
- package/dist/utils/parsing/index.js +2 -2
- package/dist/utils/parsing/jsonParser.d.ts +17 -48
- package/dist/utils/parsing/jsonParser.js +38 -79
- package/dist/utils/security/idGenerator.d.ts +40 -74
- package/dist/utils/security/idGenerator.js +63 -85
- package/dist/utils/security/index.d.ts +3 -3
- package/dist/utils/security/index.js +3 -3
- package/dist/utils/security/rateLimiter.d.ts +27 -64
- package/dist/utils/security/rateLimiter.js +24 -49
- package/dist/utils/security/sanitization.d.ts +70 -112
- package/dist/utils/security/sanitization.js +88 -151
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# MCP TypeScript Template 🚀
|
|
2
2
|
|
|
3
3
|
[](https://www.typescriptlang.org/)
|
|
4
|
-
[](https://github.com/modelcontextprotocol/typescript-sdk)
|
|
5
5
|
[](https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/changelog.mdx)
|
|
6
|
-
[](./CHANGELOG.md)
|
|
7
7
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
8
8
|
[](https://github.com/cyanheads/mcp-ts-template/issues)
|
|
9
9
|
[](https://github.com/cyanheads/mcp-ts-template)
|
package/dist/config/index.d.ts
CHANGED
|
@@ -12,150 +12,66 @@
|
|
|
12
12
|
* - Construct and export a comprehensive `config` object.
|
|
13
13
|
* - Export individual configuration values like `logLevel` and `environment` for convenience.
|
|
14
14
|
*
|
|
15
|
-
* @module config/index
|
|
15
|
+
* @module src/config/index
|
|
16
16
|
*/
|
|
17
17
|
/**
|
|
18
18
|
* Main application configuration object.
|
|
19
|
-
*
|
|
20
|
-
* and `package.json` (`pkg`), providing a single source of truth for configuration
|
|
21
|
-
* throughout the application.
|
|
22
|
-
*
|
|
23
|
-
* @constant {object} config
|
|
19
|
+
* Aggregates settings from validated environment variables and `package.json`.
|
|
24
20
|
*/
|
|
25
21
|
export declare const config: {
|
|
26
|
-
/**
|
|
27
|
-
* The name of the MCP server.
|
|
28
|
-
* Prioritizes the `MCP_SERVER_NAME` environment variable.
|
|
29
|
-
* If `MCP_SERVER_NAME` is not set, it falls back to the `name` property from `package.json`.
|
|
30
|
-
* If `package.json` is also unavailable, it uses the hardcoded default 'mcp-ts-template'.
|
|
31
|
-
* @type {string}
|
|
32
|
-
*/
|
|
22
|
+
/** MCP server name. Env `MCP_SERVER_NAME` > `package.json` name > "mcp-ts-template". */
|
|
33
23
|
mcpServerName: string;
|
|
34
|
-
/**
|
|
35
|
-
* The version of the MCP server.
|
|
36
|
-
* Prioritizes the `MCP_SERVER_VERSION` environment variable.
|
|
37
|
-
* If `MCP_SERVER_VERSION` is not set, it falls back to the `version` property from `package.json`.
|
|
38
|
-
* If `package.json` is also unavailable, it uses the hardcoded default '0.0.0'.
|
|
39
|
-
* @type {string}
|
|
40
|
-
*/
|
|
24
|
+
/** MCP server version. Env `MCP_SERVER_VERSION` > `package.json` version > "0.0.0". */
|
|
41
25
|
mcpServerVersion: string;
|
|
42
|
-
/**
|
|
43
|
-
* Logging level for the application (e.g., "debug", "info", "warning", "error").
|
|
44
|
-
* Sourced from the `MCP_LOG_LEVEL` environment variable.
|
|
45
|
-
* Defaults to "info" if `MCP_LOG_LEVEL` is not set (as defined in `EnvSchema`).
|
|
46
|
-
* @type {string}
|
|
47
|
-
*/
|
|
26
|
+
/** Logging level. From `MCP_LOG_LEVEL` env var. Default: "debug". */
|
|
48
27
|
logLevel: string;
|
|
49
|
-
/**
|
|
50
|
-
* The runtime environment of the application (e.g., "development", "production").
|
|
51
|
-
* Sourced from the `NODE_ENV` environment variable.
|
|
52
|
-
* Defaults to "development" if `NODE_ENV` is not set (as defined in `EnvSchema`).
|
|
53
|
-
* @type {string}
|
|
54
|
-
*/
|
|
28
|
+
/** Runtime environment. From `NODE_ENV` env var. Default: "development". */
|
|
55
29
|
environment: string;
|
|
56
|
-
/**
|
|
57
|
-
* Specifies the transport mechanism for MCP server communication ('stdio' or 'http').
|
|
58
|
-
* Sourced from the `MCP_TRANSPORT_TYPE` environment variable.
|
|
59
|
-
* Defaults to "stdio" if `MCP_TRANSPORT_TYPE` is not set (as defined in `EnvSchema`).
|
|
60
|
-
* @type {"stdio" | "http"}
|
|
61
|
-
*/
|
|
30
|
+
/** MCP transport type ('stdio' or 'http'). From `MCP_TRANSPORT_TYPE` env var. Default: "stdio". */
|
|
62
31
|
mcpTransportType: "stdio" | "http";
|
|
63
|
-
/**
|
|
64
|
-
* The port number for the HTTP server to listen on.
|
|
65
|
-
* Only applicable if `mcpTransportType` is 'http'.
|
|
66
|
-
* Sourced from the `MCP_HTTP_PORT` environment variable.
|
|
67
|
-
* Defaults to 3010 if `MCP_HTTP_PORT` is not set (as defined in `EnvSchema`).
|
|
68
|
-
* @type {number}
|
|
69
|
-
*/
|
|
32
|
+
/** HTTP server port (if http transport). From `MCP_HTTP_PORT` env var. Default: 3010. */
|
|
70
33
|
mcpHttpPort: number;
|
|
71
|
-
/**
|
|
72
|
-
* The host address for the HTTP server to bind to (e.g., "127.0.0.1", "0.0.0.0").
|
|
73
|
-
* Only applicable if `mcpTransportType` is 'http'.
|
|
74
|
-
* Sourced from the `MCP_HTTP_HOST` environment variable.
|
|
75
|
-
* Defaults to "127.0.0.1" if `MCP_HTTP_HOST` is not set (as defined in `EnvSchema`).
|
|
76
|
-
* @type {string}
|
|
77
|
-
*/
|
|
34
|
+
/** HTTP server host (if http transport). From `MCP_HTTP_HOST` env var. Default: "127.0.0.1". */
|
|
78
35
|
mcpHttpHost: string;
|
|
79
|
-
/**
|
|
80
|
-
* An array of allowed origins for CORS requests when using the 'http' transport.
|
|
81
|
-
* Derived from the `MCP_ALLOWED_ORIGINS` environment variable (comma-separated string).
|
|
82
|
-
* Each origin string is trimmed. Empty strings resulting from multiple commas are filtered out.
|
|
83
|
-
* If `MCP_ALLOWED_ORIGINS` is not set, this will be `undefined`.
|
|
84
|
-
* The HTTP transport layer should handle the `undefined` case appropriately (e.g., by applying default CORS policies).
|
|
85
|
-
* @type {string[] | undefined}
|
|
86
|
-
*/
|
|
36
|
+
/** Array of allowed CORS origins (http transport). From `MCP_ALLOWED_ORIGINS` (comma-separated). */
|
|
87
37
|
mcpAllowedOrigins: string[] | undefined;
|
|
88
|
-
/**
|
|
89
|
-
* The secret key used for signing and verifying authentication tokens (e.g., JWTs).
|
|
90
|
-
* This is critical for securing the HTTP transport if authentication is enabled.
|
|
91
|
-
* Sourced from the `MCP_AUTH_SECRET_KEY` environment variable.
|
|
92
|
-
* It's crucial that this key is kept confidential and is sufficiently complex.
|
|
93
|
-
* The `EnvSchema` enforces a minimum length of 32 characters.
|
|
94
|
-
* If not set, this will be `undefined`. The authentication middleware should handle this,
|
|
95
|
-
* potentially throwing an error if it's required but missing, especially in production.
|
|
96
|
-
* @type {string | undefined}
|
|
97
|
-
*/
|
|
38
|
+
/** Auth secret key (JWTs, http transport). From `MCP_AUTH_SECRET_KEY`. CRITICAL. */
|
|
98
39
|
mcpAuthSecretKey: string | undefined;
|
|
99
|
-
/**
|
|
100
|
-
* The application URL for OpenRouter integration.
|
|
101
|
-
* Defaults to "http://localhost:3000" if `OPENROUTER_APP_URL` is not set.
|
|
102
|
-
* @type {string}
|
|
103
|
-
*/
|
|
40
|
+
/** OpenRouter App URL. From `OPENROUTER_APP_URL`. Default: "http://localhost:3000". */
|
|
104
41
|
openrouterAppUrl: string;
|
|
105
|
-
/**
|
|
106
|
-
* The application name for OpenRouter integration.
|
|
107
|
-
* Defaults to `mcpServerName` (which itself defaults to `pkg.name` or "MCP TS App") if `OPENROUTER_APP_NAME` is not set.
|
|
108
|
-
* @type {string}
|
|
109
|
-
*/
|
|
42
|
+
/** OpenRouter App Name. From `OPENROUTER_APP_NAME`. Defaults to `mcpServerName`. */
|
|
110
43
|
openrouterAppName: string;
|
|
111
|
-
/**
|
|
112
|
-
* The API key for OpenRouter services.
|
|
113
|
-
* Sourced from `OPENROUTER_API_KEY` environment variable. No default is provided here;
|
|
114
|
-
* the service using this key should handle its absence (e.g., by disabling features or erroring).
|
|
115
|
-
* @type {string | undefined}
|
|
116
|
-
*/
|
|
44
|
+
/** OpenRouter API Key. From `OPENROUTER_API_KEY`. */
|
|
117
45
|
openrouterApiKey: string | undefined;
|
|
118
|
-
/**
|
|
119
|
-
* Default model for LLM operations.
|
|
120
|
-
* @type {string}
|
|
121
|
-
*/
|
|
46
|
+
/** Default LLM model. From `LLM_DEFAULT_MODEL`. */
|
|
122
47
|
llmDefaultModel: string;
|
|
123
|
-
/**
|
|
124
|
-
* Default temperature for LLM sampling.
|
|
125
|
-
* @type {number | undefined}
|
|
126
|
-
*/
|
|
48
|
+
/** Default LLM temperature. From `LLM_DEFAULT_TEMPERATURE`. */
|
|
127
49
|
llmDefaultTemperature: number | undefined;
|
|
128
|
-
/**
|
|
129
|
-
* Default top_p for LLM sampling.
|
|
130
|
-
* @type {number | undefined}
|
|
131
|
-
*/
|
|
50
|
+
/** Default LLM top_p. From `LLM_DEFAULT_TOP_P`. */
|
|
132
51
|
llmDefaultTopP: number | undefined;
|
|
133
|
-
/**
|
|
134
|
-
* Default maximum tokens for LLM responses.
|
|
135
|
-
* @type {number | undefined}
|
|
136
|
-
*/
|
|
52
|
+
/** Default LLM max tokens. From `LLM_DEFAULT_MAX_TOKENS`. */
|
|
137
53
|
llmDefaultMaxTokens: number | undefined;
|
|
138
|
-
/**
|
|
139
|
-
* Default top_k for LLM sampling.
|
|
140
|
-
* @type {number | undefined}
|
|
141
|
-
*/
|
|
54
|
+
/** Default LLM top_k. From `LLM_DEFAULT_TOP_K`. */
|
|
142
55
|
llmDefaultTopK: number | undefined;
|
|
143
|
-
/**
|
|
144
|
-
* Default min_p for LLM sampling.
|
|
145
|
-
* @type {number | undefined}
|
|
146
|
-
*/
|
|
56
|
+
/** Default LLM min_p. From `LLM_DEFAULT_MIN_P`. */
|
|
147
57
|
llmDefaultMinP: number | undefined;
|
|
58
|
+
/** OAuth Proxy configurations. Undefined if no related env vars are set. */
|
|
59
|
+
oauthProxy: {
|
|
60
|
+
authorizationUrl: string | undefined;
|
|
61
|
+
tokenUrl: string | undefined;
|
|
62
|
+
revocationUrl: string | undefined;
|
|
63
|
+
issuerUrl: string | undefined;
|
|
64
|
+
serviceDocumentationUrl: string | undefined;
|
|
65
|
+
defaultClientRedirectUris: string[] | undefined;
|
|
66
|
+
} | undefined;
|
|
148
67
|
};
|
|
149
68
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* allowing direct import where only the log level is needed (e.g., during logger initialization).
|
|
153
|
-
* @type {string}
|
|
69
|
+
* Configured logging level for the application.
|
|
70
|
+
* Exported for convenience.
|
|
154
71
|
*/
|
|
155
72
|
export declare const logLevel: string;
|
|
156
73
|
/**
|
|
157
|
-
*
|
|
158
|
-
* Exported
|
|
159
|
-
* @type {string}
|
|
74
|
+
* Configured runtime environment ("development", "production", etc.).
|
|
75
|
+
* Exported for convenience.
|
|
160
76
|
*/
|
|
161
77
|
export declare const environment: string;
|
package/dist/config/index.js
CHANGED
|
@@ -12,357 +12,180 @@
|
|
|
12
12
|
* - Construct and export a comprehensive `config` object.
|
|
13
13
|
* - Export individual configuration values like `logLevel` and `environment` for convenience.
|
|
14
14
|
*
|
|
15
|
-
* @module config/index
|
|
15
|
+
* @module src/config/index
|
|
16
16
|
*/
|
|
17
17
|
import dotenv from "dotenv";
|
|
18
18
|
import { readFileSync } from "fs";
|
|
19
19
|
import { dirname, join } from "path";
|
|
20
20
|
import { fileURLToPath } from "url";
|
|
21
21
|
import { z } from "zod";
|
|
22
|
-
// Load environment variables from .env file at the earliest stage.
|
|
23
|
-
// dotenv.config() will read the .env file in the root of the project,
|
|
24
|
-
// parse its contents, and assign them to process.env.
|
|
25
22
|
dotenv.config();
|
|
26
|
-
// --- Determine Package Information ---
|
|
27
|
-
// Get the directory name of the current module using ES module equivalents.
|
|
28
|
-
// import.meta.url gives the URL of the current module file.
|
|
29
|
-
// fileURLToPath converts this file URL to an absolute path.
|
|
30
|
-
// dirname gets the directory part of that path.
|
|
31
23
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
32
|
-
// Construct the absolute path to package.json, assuming it's two levels up from this config file.
|
|
33
|
-
// This makes the path resolution robust to where the script is run from.
|
|
34
24
|
const pkgPath = join(__dirname, "../../package.json");
|
|
35
|
-
// Default package information to be used if package.json is unreadable or missing.
|
|
36
|
-
// This prevents the application from crashing if package.json is not found.
|
|
37
25
|
let pkg = { name: "mcp-ts-template", version: "0.0.0" };
|
|
38
26
|
try {
|
|
39
|
-
// Attempt to read and parse package.json to get actual server name and version.
|
|
40
|
-
// These values can serve as defaults if not overridden by environment variables.
|
|
41
27
|
pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
42
28
|
}
|
|
43
29
|
catch (error) {
|
|
44
|
-
// Silently use default pkg info if reading fails.
|
|
45
|
-
// In a production environment, more robust error handling or logging might be desired.
|
|
46
|
-
// For example, logging this error to a file or a monitoring service.
|
|
47
30
|
if (process.stdout.isTTY) {
|
|
48
|
-
// Guarded console.error: Only log to console if it's an interactive terminal.
|
|
49
|
-
// This avoids polluting stdout/stderr in scripted environments.
|
|
50
31
|
console.error("Warning: Could not read package.json for default config values. Using hardcoded defaults.", error);
|
|
51
32
|
}
|
|
52
33
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// 4. Clear error messages: Zod's error reporting is helpful for debugging.
|
|
34
|
+
/**
|
|
35
|
+
* Zod schema for validating environment variables.
|
|
36
|
+
* Provides type safety, validation, defaults, and clear error messages.
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
59
39
|
const EnvSchema = z.object({
|
|
60
|
-
/**
|
|
61
|
-
* Optional. The desired name for the MCP server.
|
|
62
|
-
* If not set, the 'name' field from `package.json` will be used.
|
|
63
|
-
* @type {string | undefined}
|
|
64
|
-
*/
|
|
40
|
+
/** Optional. The desired name for the MCP server. Defaults to `package.json` name. */
|
|
65
41
|
MCP_SERVER_NAME: z.string().optional(),
|
|
66
|
-
/**
|
|
67
|
-
* Optional. The version of the MCP server.
|
|
68
|
-
* If not set, the 'version' field from `package.json` will be used.
|
|
69
|
-
* @type {string | undefined}
|
|
70
|
-
*/
|
|
42
|
+
/** Optional. The version of the MCP server. Defaults to `package.json` version. */
|
|
71
43
|
MCP_SERVER_VERSION: z.string().optional(),
|
|
72
|
-
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
* See `McpLogLevel` in logger utility for possible values.
|
|
76
|
-
* @type {string}
|
|
77
|
-
* @default "info"
|
|
78
|
-
*/
|
|
79
|
-
MCP_LOG_LEVEL: z.string().default("debug"), // Forced to debug for testing
|
|
80
|
-
/**
|
|
81
|
-
* The runtime environment of the application (e.g., "development", "production", "test").
|
|
82
|
-
* @type {string}
|
|
83
|
-
* @default "development"
|
|
84
|
-
*/
|
|
44
|
+
/** Minimum logging level. See `McpLogLevel` in logger utility. Default: "debug". */
|
|
45
|
+
MCP_LOG_LEVEL: z.string().default("debug"),
|
|
46
|
+
/** Runtime environment (e.g., "development", "production"). Default: "development". */
|
|
85
47
|
NODE_ENV: z.string().default("development"),
|
|
86
|
-
/**
|
|
87
|
-
* Specifies the transport mechanism for MCP communication.
|
|
88
|
-
* - "stdio": Uses standard input/output.
|
|
89
|
-
* - "http": Uses HTTP (typically with Server-Sent Events).
|
|
90
|
-
* @type {"stdio" | "http"}
|
|
91
|
-
* @default "stdio"
|
|
92
|
-
*/
|
|
48
|
+
/** MCP communication transport ("stdio" or "http"). Default: "stdio". */
|
|
93
49
|
MCP_TRANSPORT_TYPE: z.enum(["stdio", "http"]).default("stdio"),
|
|
94
|
-
/**
|
|
95
|
-
* The port number for the HTTP server to listen on.
|
|
96
|
-
* Only applicable if `MCP_TRANSPORT_TYPE` is "http".
|
|
97
|
-
* Must be a positive integer.
|
|
98
|
-
* @type {number}
|
|
99
|
-
* @default 3010
|
|
100
|
-
*/
|
|
50
|
+
/** HTTP server port (if MCP_TRANSPORT_TYPE is "http"). Default: 3010. */
|
|
101
51
|
MCP_HTTP_PORT: z.coerce.number().int().positive().default(3010),
|
|
102
|
-
/**
|
|
103
|
-
* The host address for the HTTP server to bind to.
|
|
104
|
-
* Only applicable if `MCP_TRANSPORT_TYPE` is "http".
|
|
105
|
-
* @type {string}
|
|
106
|
-
* @default "127.0.0.1" (localhost)
|
|
107
|
-
*/
|
|
52
|
+
/** HTTP server host (if MCP_TRANSPORT_TYPE is "http"). Default: "127.0.0.1". */
|
|
108
53
|
MCP_HTTP_HOST: z.string().default("127.0.0.1"),
|
|
109
|
-
/**
|
|
110
|
-
* Optional. A comma-separated string of allowed origins for CORS (Cross-Origin Resource Sharing)
|
|
111
|
-
* when `MCP_TRANSPORT_TYPE` is "http". E.g., "http://localhost:8080,https://my-frontend.com".
|
|
112
|
-
* If not set, the HTTP transport layer might enforce stricter default CORS policies.
|
|
113
|
-
* @type {string | undefined}
|
|
114
|
-
*/
|
|
54
|
+
/** Optional. Comma-separated allowed origins for CORS (HTTP transport). */
|
|
115
55
|
MCP_ALLOWED_ORIGINS: z.string().optional(),
|
|
116
|
-
/**
|
|
117
|
-
* Optional but **HIGHLY RECOMMENDED for HTTP transport in production**.
|
|
118
|
-
* A secret key (minimum 32 characters) used for signing and verifying authentication tokens (e.g., JWTs).
|
|
119
|
-
* If not set for HTTP transport, the authentication middleware should ideally prevent startup in production.
|
|
120
|
-
* @type {string | undefined}
|
|
121
|
-
*/
|
|
56
|
+
/** Optional. Secret key (min 32 chars) for auth tokens (HTTP transport). CRITICAL for production. */
|
|
122
57
|
MCP_AUTH_SECRET_KEY: z
|
|
123
58
|
.string()
|
|
124
59
|
.min(32, "MCP_AUTH_SECRET_KEY must be at least 32 characters long for security reasons.")
|
|
125
60
|
.optional(),
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Optional. The application URL for OpenRouter integration, typically your frontend URL.
|
|
129
|
-
* Used for constructing callback URLs or identifying the application.
|
|
130
|
-
* @type {string | undefined}
|
|
131
|
-
*/
|
|
61
|
+
/** Optional. Application URL for OpenRouter integration. */
|
|
132
62
|
OPENROUTER_APP_URL: z
|
|
133
63
|
.string()
|
|
134
64
|
.url("OPENROUTER_APP_URL must be a valid URL (e.g., http://localhost:3000)")
|
|
135
65
|
.optional(),
|
|
136
|
-
/**
|
|
137
|
-
* Optional. The application name for OpenRouter integration.
|
|
138
|
-
* Defaults to the `MCP_SERVER_NAME` or `package.json` name if not set.
|
|
139
|
-
* @type {string | undefined}
|
|
140
|
-
*/
|
|
66
|
+
/** Optional. Application name for OpenRouter. Defaults to MCP_SERVER_NAME or package name. */
|
|
141
67
|
OPENROUTER_APP_NAME: z.string().optional(),
|
|
142
|
-
/**
|
|
143
|
-
* Optional. The API key for accessing OpenRouter services.
|
|
144
|
-
* If not provided, features requiring OpenRouter authentication will be disabled or may fail.
|
|
145
|
-
* @type {string | undefined}
|
|
146
|
-
*/
|
|
68
|
+
/** Optional. API key for OpenRouter services. */
|
|
147
69
|
OPENROUTER_API_KEY: z.string().optional(),
|
|
148
|
-
/**
|
|
149
|
-
* The default language model to be used for LLM operations if not specified otherwise.
|
|
150
|
-
* The format is typically `provider/model-name:version-tag`.
|
|
151
|
-
* @type {string}
|
|
152
|
-
* @default "google/gemini-2.5-flash-preview:thinking"
|
|
153
|
-
*/
|
|
70
|
+
/** Default LLM model. Default: "google/gemini-2.5-flash-preview:thinking". */
|
|
154
71
|
LLM_DEFAULT_MODEL: z
|
|
155
72
|
.string()
|
|
156
|
-
.default("google/gemini-2.5-flash-preview:thinking"),
|
|
157
|
-
/**
|
|
158
|
-
* Optional. The default temperature for LLM sampling (controls randomness).
|
|
159
|
-
* Must be between 0.0 and 2.0.
|
|
160
|
-
* @type {number | undefined}
|
|
161
|
-
*/
|
|
73
|
+
.default("google/gemini-2.5-flash-preview:thinking"),
|
|
74
|
+
/** Optional. Default LLM temperature (0.0-2.0). */
|
|
162
75
|
LLM_DEFAULT_TEMPERATURE: z.coerce.number().min(0).max(2).optional(),
|
|
163
|
-
/**
|
|
164
|
-
* Optional. The default top_p for LLM sampling (nucleus sampling).
|
|
165
|
-
* Must be between 0.0 and 1.0.
|
|
166
|
-
* @type {number | undefined}
|
|
167
|
-
*/
|
|
76
|
+
/** Optional. Default LLM top_p (0.0-1.0). */
|
|
168
77
|
LLM_DEFAULT_TOP_P: z.coerce.number().min(0).max(1).optional(),
|
|
169
|
-
/**
|
|
170
|
-
* Optional. The default maximum number of tokens to generate in LLM responses.
|
|
171
|
-
* Must be a positive integer.
|
|
172
|
-
* @type {number | undefined}
|
|
173
|
-
*/
|
|
78
|
+
/** Optional. Default LLM max tokens (positive integer). */
|
|
174
79
|
LLM_DEFAULT_MAX_TOKENS: z.coerce.number().int().positive().optional(),
|
|
175
|
-
/**
|
|
176
|
-
* Optional. The default top_k for LLM sampling.
|
|
177
|
-
* Must be a non-negative integer (0 means it's not used).
|
|
178
|
-
* @type {number | undefined}
|
|
179
|
-
*/
|
|
80
|
+
/** Optional. Default LLM top_k (non-negative integer). */
|
|
180
81
|
LLM_DEFAULT_TOP_K: z.coerce.number().int().nonnegative().optional(),
|
|
181
|
-
/**
|
|
182
|
-
* Optional. The default min_p for LLM sampling.
|
|
183
|
-
* Must be between 0.0 and 1.0.
|
|
184
|
-
* @type {number | undefined}
|
|
185
|
-
*/
|
|
82
|
+
/** Optional. Default LLM min_p (0.0-1.0). */
|
|
186
83
|
LLM_DEFAULT_MIN_P: z.coerce.number().min(0).max(1).optional(),
|
|
84
|
+
/** Optional. OAuth provider authorization endpoint URL. */
|
|
85
|
+
OAUTH_PROXY_AUTHORIZATION_URL: z
|
|
86
|
+
.string()
|
|
87
|
+
.url("OAUTH_PROXY_AUTHORIZATION_URL must be a valid URL.")
|
|
88
|
+
.optional(),
|
|
89
|
+
/** Optional. OAuth provider token endpoint URL. */
|
|
90
|
+
OAUTH_PROXY_TOKEN_URL: z
|
|
91
|
+
.string()
|
|
92
|
+
.url("OAUTH_PROXY_TOKEN_URL must be a valid URL.")
|
|
93
|
+
.optional(),
|
|
94
|
+
/** Optional. OAuth provider revocation endpoint URL. */
|
|
95
|
+
OAUTH_PROXY_REVOCATION_URL: z
|
|
96
|
+
.string()
|
|
97
|
+
.url("OAUTH_PROXY_REVOCATION_URL must be a valid URL.")
|
|
98
|
+
.optional(),
|
|
99
|
+
/** Optional. OAuth provider issuer URL. */
|
|
100
|
+
OAUTH_PROXY_ISSUER_URL: z
|
|
101
|
+
.string()
|
|
102
|
+
.url("OAUTH_PROXY_ISSUER_URL must be a valid URL.")
|
|
103
|
+
.optional(),
|
|
104
|
+
/** Optional. OAuth service documentation URL. */
|
|
105
|
+
OAUTH_PROXY_SERVICE_DOCUMENTATION_URL: z
|
|
106
|
+
.string()
|
|
107
|
+
.url("OAUTH_PROXY_SERVICE_DOCUMENTATION_URL must be a valid URL.")
|
|
108
|
+
.optional(),
|
|
109
|
+
/** Optional. Comma-separated default OAuth client redirect URIs. */
|
|
110
|
+
OAUTH_PROXY_DEFAULT_CLIENT_REDIRECT_URIS: z.string().optional(),
|
|
187
111
|
});
|
|
188
|
-
// --- Parse and Validate Environment Variables ---
|
|
189
|
-
// `safeParse` attempts to parse `process.env` against `EnvSchema`.
|
|
190
|
-
// It returns a result object indicating success or failure, along with parsed data or error details.
|
|
191
112
|
const parsedEnv = EnvSchema.safeParse(process.env);
|
|
192
113
|
if (!parsedEnv.success) {
|
|
193
|
-
// If parsing fails, log the specific validation errors to the console if it's an interactive TTY.
|
|
194
|
-
// This helps developers quickly identify misconfigured environment variables.
|
|
195
114
|
if (process.stdout.isTTY) {
|
|
196
|
-
console.error("❌ Invalid environment variables found:",
|
|
197
|
-
// `flatten().fieldErrors` provides a user-friendly structure of errors per field.
|
|
198
|
-
parsedEnv.error.flatten().fieldErrors);
|
|
115
|
+
console.error("❌ Invalid environment variables found:", parsedEnv.error.flatten().fieldErrors);
|
|
199
116
|
}
|
|
200
|
-
//
|
|
201
|
-
// - For critical configurations (e.g., database URL in production), throwing an error and exiting is safer.
|
|
202
|
-
// - For less critical ones, or in development, proceeding with defaults (as done here) might be acceptable.
|
|
203
|
-
// This template currently logs the error and attempts to use defaults.
|
|
204
|
-
// Consider `throw new Error("Invalid or missing critical environment configuration.");` for production.
|
|
117
|
+
// Consider throwing an error in production for critical misconfigurations.
|
|
205
118
|
}
|
|
206
|
-
// If parsing was successful, use `parsedEnv.data`.
|
|
207
|
-
// Otherwise, `EnvSchema.parse({})` will parse an empty object, effectively applying all default values
|
|
208
|
-
// defined in the schema. This ensures `env` is always a valid object according to `EnvSchema`.
|
|
209
119
|
const env = parsedEnv.success ? parsedEnv.data : EnvSchema.parse({});
|
|
210
120
|
/**
|
|
211
121
|
* Main application configuration object.
|
|
212
|
-
*
|
|
213
|
-
* and `package.json` (`pkg`), providing a single source of truth for configuration
|
|
214
|
-
* throughout the application.
|
|
215
|
-
*
|
|
216
|
-
* @constant {object} config
|
|
122
|
+
* Aggregates settings from validated environment variables and `package.json`.
|
|
217
123
|
*/
|
|
218
124
|
export const config = {
|
|
219
|
-
/**
|
|
220
|
-
* The name of the MCP server.
|
|
221
|
-
* Prioritizes the `MCP_SERVER_NAME` environment variable.
|
|
222
|
-
* If `MCP_SERVER_NAME` is not set, it falls back to the `name` property from `package.json`.
|
|
223
|
-
* If `package.json` is also unavailable, it uses the hardcoded default 'mcp-ts-template'.
|
|
224
|
-
* @type {string}
|
|
225
|
-
*/
|
|
125
|
+
/** MCP server name. Env `MCP_SERVER_NAME` > `package.json` name > "mcp-ts-template". */
|
|
226
126
|
mcpServerName: env.MCP_SERVER_NAME || pkg.name,
|
|
227
|
-
/**
|
|
228
|
-
* The version of the MCP server.
|
|
229
|
-
* Prioritizes the `MCP_SERVER_VERSION` environment variable.
|
|
230
|
-
* If `MCP_SERVER_VERSION` is not set, it falls back to the `version` property from `package.json`.
|
|
231
|
-
* If `package.json` is also unavailable, it uses the hardcoded default '0.0.0'.
|
|
232
|
-
* @type {string}
|
|
233
|
-
*/
|
|
127
|
+
/** MCP server version. Env `MCP_SERVER_VERSION` > `package.json` version > "0.0.0". */
|
|
234
128
|
mcpServerVersion: env.MCP_SERVER_VERSION || pkg.version,
|
|
235
|
-
/**
|
|
236
|
-
* Logging level for the application (e.g., "debug", "info", "warning", "error").
|
|
237
|
-
* Sourced from the `MCP_LOG_LEVEL` environment variable.
|
|
238
|
-
* Defaults to "info" if `MCP_LOG_LEVEL` is not set (as defined in `EnvSchema`).
|
|
239
|
-
* @type {string}
|
|
240
|
-
*/
|
|
129
|
+
/** Logging level. From `MCP_LOG_LEVEL` env var. Default: "debug". */
|
|
241
130
|
logLevel: env.MCP_LOG_LEVEL,
|
|
242
|
-
/**
|
|
243
|
-
* The runtime environment of the application (e.g., "development", "production").
|
|
244
|
-
* Sourced from the `NODE_ENV` environment variable.
|
|
245
|
-
* Defaults to "development" if `NODE_ENV` is not set (as defined in `EnvSchema`).
|
|
246
|
-
* @type {string}
|
|
247
|
-
*/
|
|
131
|
+
/** Runtime environment. From `NODE_ENV` env var. Default: "development". */
|
|
248
132
|
environment: env.NODE_ENV,
|
|
249
|
-
/**
|
|
250
|
-
* Specifies the transport mechanism for MCP server communication ('stdio' or 'http').
|
|
251
|
-
* Sourced from the `MCP_TRANSPORT_TYPE` environment variable.
|
|
252
|
-
* Defaults to "stdio" if `MCP_TRANSPORT_TYPE` is not set (as defined in `EnvSchema`).
|
|
253
|
-
* @type {"stdio" | "http"}
|
|
254
|
-
*/
|
|
133
|
+
/** MCP transport type ('stdio' or 'http'). From `MCP_TRANSPORT_TYPE` env var. Default: "stdio". */
|
|
255
134
|
mcpTransportType: env.MCP_TRANSPORT_TYPE,
|
|
256
|
-
/**
|
|
257
|
-
* The port number for the HTTP server to listen on.
|
|
258
|
-
* Only applicable if `mcpTransportType` is 'http'.
|
|
259
|
-
* Sourced from the `MCP_HTTP_PORT` environment variable.
|
|
260
|
-
* Defaults to 3010 if `MCP_HTTP_PORT` is not set (as defined in `EnvSchema`).
|
|
261
|
-
* @type {number}
|
|
262
|
-
*/
|
|
135
|
+
/** HTTP server port (if http transport). From `MCP_HTTP_PORT` env var. Default: 3010. */
|
|
263
136
|
mcpHttpPort: env.MCP_HTTP_PORT,
|
|
264
|
-
/**
|
|
265
|
-
* The host address for the HTTP server to bind to (e.g., "127.0.0.1", "0.0.0.0").
|
|
266
|
-
* Only applicable if `mcpTransportType` is 'http'.
|
|
267
|
-
* Sourced from the `MCP_HTTP_HOST` environment variable.
|
|
268
|
-
* Defaults to "127.0.0.1" if `MCP_HTTP_HOST` is not set (as defined in `EnvSchema`).
|
|
269
|
-
* @type {string}
|
|
270
|
-
*/
|
|
137
|
+
/** HTTP server host (if http transport). From `MCP_HTTP_HOST` env var. Default: "127.0.0.1". */
|
|
271
138
|
mcpHttpHost: env.MCP_HTTP_HOST,
|
|
272
|
-
/**
|
|
273
|
-
* An array of allowed origins for CORS requests when using the 'http' transport.
|
|
274
|
-
* Derived from the `MCP_ALLOWED_ORIGINS` environment variable (comma-separated string).
|
|
275
|
-
* Each origin string is trimmed. Empty strings resulting from multiple commas are filtered out.
|
|
276
|
-
* If `MCP_ALLOWED_ORIGINS` is not set, this will be `undefined`.
|
|
277
|
-
* The HTTP transport layer should handle the `undefined` case appropriately (e.g., by applying default CORS policies).
|
|
278
|
-
* @type {string[] | undefined}
|
|
279
|
-
*/
|
|
139
|
+
/** Array of allowed CORS origins (http transport). From `MCP_ALLOWED_ORIGINS` (comma-separated). */
|
|
280
140
|
mcpAllowedOrigins: env.MCP_ALLOWED_ORIGINS?.split(",")
|
|
281
|
-
.map((origin) => origin.trim())
|
|
282
|
-
.filter(Boolean),
|
|
283
|
-
/**
|
|
284
|
-
* The secret key used for signing and verifying authentication tokens (e.g., JWTs).
|
|
285
|
-
* This is critical for securing the HTTP transport if authentication is enabled.
|
|
286
|
-
* Sourced from the `MCP_AUTH_SECRET_KEY` environment variable.
|
|
287
|
-
* It's crucial that this key is kept confidential and is sufficiently complex.
|
|
288
|
-
* The `EnvSchema` enforces a minimum length of 32 characters.
|
|
289
|
-
* If not set, this will be `undefined`. The authentication middleware should handle this,
|
|
290
|
-
* potentially throwing an error if it's required but missing, especially in production.
|
|
291
|
-
* @type {string | undefined}
|
|
292
|
-
*/
|
|
141
|
+
.map((origin) => origin.trim())
|
|
142
|
+
.filter(Boolean),
|
|
143
|
+
/** Auth secret key (JWTs, http transport). From `MCP_AUTH_SECRET_KEY`. CRITICAL. */
|
|
293
144
|
mcpAuthSecretKey: env.MCP_AUTH_SECRET_KEY,
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* The application URL for OpenRouter integration.
|
|
297
|
-
* Defaults to "http://localhost:3000" if `OPENROUTER_APP_URL` is not set.
|
|
298
|
-
* @type {string}
|
|
299
|
-
*/
|
|
145
|
+
/** OpenRouter App URL. From `OPENROUTER_APP_URL`. Default: "http://localhost:3000". */
|
|
300
146
|
openrouterAppUrl: env.OPENROUTER_APP_URL || "http://localhost:3000",
|
|
301
|
-
/**
|
|
302
|
-
* The application name for OpenRouter integration.
|
|
303
|
-
* Defaults to `mcpServerName` (which itself defaults to `pkg.name` or "MCP TS App") if `OPENROUTER_APP_NAME` is not set.
|
|
304
|
-
* @type {string}
|
|
305
|
-
*/
|
|
147
|
+
/** OpenRouter App Name. From `OPENROUTER_APP_NAME`. Defaults to `mcpServerName`. */
|
|
306
148
|
openrouterAppName: env.OPENROUTER_APP_NAME || pkg.name || "MCP TS App",
|
|
307
|
-
/**
|
|
308
|
-
* The API key for OpenRouter services.
|
|
309
|
-
* Sourced from `OPENROUTER_API_KEY` environment variable. No default is provided here;
|
|
310
|
-
* the service using this key should handle its absence (e.g., by disabling features or erroring).
|
|
311
|
-
* @type {string | undefined}
|
|
312
|
-
*/
|
|
149
|
+
/** OpenRouter API Key. From `OPENROUTER_API_KEY`. */
|
|
313
150
|
openrouterApiKey: env.OPENROUTER_API_KEY,
|
|
314
|
-
/**
|
|
315
|
-
* Default model for LLM operations.
|
|
316
|
-
* @type {string}
|
|
317
|
-
*/
|
|
151
|
+
/** Default LLM model. From `LLM_DEFAULT_MODEL`. */
|
|
318
152
|
llmDefaultModel: env.LLM_DEFAULT_MODEL,
|
|
319
|
-
/**
|
|
320
|
-
* Default temperature for LLM sampling.
|
|
321
|
-
* @type {number | undefined}
|
|
322
|
-
*/
|
|
153
|
+
/** Default LLM temperature. From `LLM_DEFAULT_TEMPERATURE`. */
|
|
323
154
|
llmDefaultTemperature: env.LLM_DEFAULT_TEMPERATURE,
|
|
324
|
-
/**
|
|
325
|
-
* Default top_p for LLM sampling.
|
|
326
|
-
* @type {number | undefined}
|
|
327
|
-
*/
|
|
155
|
+
/** Default LLM top_p. From `LLM_DEFAULT_TOP_P`. */
|
|
328
156
|
llmDefaultTopP: env.LLM_DEFAULT_TOP_P,
|
|
329
|
-
/**
|
|
330
|
-
* Default maximum tokens for LLM responses.
|
|
331
|
-
* @type {number | undefined}
|
|
332
|
-
*/
|
|
157
|
+
/** Default LLM max tokens. From `LLM_DEFAULT_MAX_TOKENS`. */
|
|
333
158
|
llmDefaultMaxTokens: env.LLM_DEFAULT_MAX_TOKENS,
|
|
334
|
-
/**
|
|
335
|
-
* Default top_k for LLM sampling.
|
|
336
|
-
* @type {number | undefined}
|
|
337
|
-
*/
|
|
159
|
+
/** Default LLM top_k. From `LLM_DEFAULT_TOP_K`. */
|
|
338
160
|
llmDefaultTopK: env.LLM_DEFAULT_TOP_K,
|
|
339
|
-
/**
|
|
340
|
-
* Default min_p for LLM sampling.
|
|
341
|
-
* @type {number | undefined}
|
|
342
|
-
*/
|
|
161
|
+
/** Default LLM min_p. From `LLM_DEFAULT_MIN_P`. */
|
|
343
162
|
llmDefaultMinP: env.LLM_DEFAULT_MIN_P,
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
163
|
+
/** OAuth Proxy configurations. Undefined if no related env vars are set. */
|
|
164
|
+
oauthProxy: env.OAUTH_PROXY_AUTHORIZATION_URL ||
|
|
165
|
+
env.OAUTH_PROXY_TOKEN_URL ||
|
|
166
|
+
env.OAUTH_PROXY_REVOCATION_URL ||
|
|
167
|
+
env.OAUTH_PROXY_ISSUER_URL ||
|
|
168
|
+
env.OAUTH_PROXY_SERVICE_DOCUMENTATION_URL ||
|
|
169
|
+
env.OAUTH_PROXY_DEFAULT_CLIENT_REDIRECT_URIS
|
|
170
|
+
? {
|
|
171
|
+
authorizationUrl: env.OAUTH_PROXY_AUTHORIZATION_URL,
|
|
172
|
+
tokenUrl: env.OAUTH_PROXY_TOKEN_URL,
|
|
173
|
+
revocationUrl: env.OAUTH_PROXY_REVOCATION_URL,
|
|
174
|
+
issuerUrl: env.OAUTH_PROXY_ISSUER_URL,
|
|
175
|
+
serviceDocumentationUrl: env.OAUTH_PROXY_SERVICE_DOCUMENTATION_URL,
|
|
176
|
+
defaultClientRedirectUris: env.OAUTH_PROXY_DEFAULT_CLIENT_REDIRECT_URIS?.split(",")
|
|
177
|
+
.map((uri) => uri.trim())
|
|
178
|
+
.filter(Boolean),
|
|
179
|
+
}
|
|
180
|
+
: undefined,
|
|
352
181
|
};
|
|
353
182
|
/**
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
* allowing direct import where only the log level is needed (e.g., during logger initialization).
|
|
357
|
-
* @type {string}
|
|
183
|
+
* Configured logging level for the application.
|
|
184
|
+
* Exported for convenience.
|
|
358
185
|
*/
|
|
359
186
|
export const logLevel = config.logLevel;
|
|
360
187
|
/**
|
|
361
|
-
*
|
|
362
|
-
* Exported
|
|
363
|
-
* @type {string}
|
|
188
|
+
* Configured runtime environment ("development", "production", etc.).
|
|
189
|
+
* Exported for convenience.
|
|
364
190
|
*/
|
|
365
191
|
export const environment = config.environment;
|
|
366
|
-
// Final reminder: Logger initialization and any validation logic that depends on the fully
|
|
367
|
-
// constructed `config` object should typically occur at the application's main entry point
|
|
368
|
-
// (e.g., `src/index.ts`) after this configuration module has been imported and processed.
|