@metronome/mcp 3.0.0 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +3 -3
  2. package/auth.d.mts +6 -0
  3. package/auth.d.mts.map +1 -0
  4. package/auth.d.ts +6 -0
  5. package/auth.d.ts.map +1 -0
  6. package/auth.js +37 -0
  7. package/auth.js.map +1 -0
  8. package/{headers.js → auth.mjs} +16 -6
  9. package/auth.mjs.map +1 -0
  10. package/code-tool-paths.cjs +6 -0
  11. package/code-tool-paths.cjs.map +1 -0
  12. package/code-tool-paths.d.cts +2 -0
  13. package/code-tool-paths.d.cts.map +1 -0
  14. package/code-tool-types.d.mts.map +1 -1
  15. package/code-tool-types.d.ts.map +1 -1
  16. package/code-tool-worker.d.mts +5 -0
  17. package/code-tool-worker.d.mts.map +1 -0
  18. package/code-tool-worker.d.ts +5 -0
  19. package/code-tool-worker.d.ts.map +1 -0
  20. package/code-tool-worker.js +341 -0
  21. package/code-tool-worker.js.map +1 -0
  22. package/code-tool-worker.mjs +336 -0
  23. package/code-tool-worker.mjs.map +1 -0
  24. package/code-tool.d.mts +11 -2
  25. package/code-tool.d.mts.map +1 -1
  26. package/code-tool.d.ts +11 -2
  27. package/code-tool.d.ts.map +1 -1
  28. package/code-tool.js +277 -41
  29. package/code-tool.js.map +1 -1
  30. package/code-tool.mjs +242 -42
  31. package/code-tool.mjs.map +1 -1
  32. package/docs-search-tool.d.mts +9 -3
  33. package/docs-search-tool.d.mts.map +1 -1
  34. package/docs-search-tool.d.ts +9 -3
  35. package/docs-search-tool.d.ts.map +1 -1
  36. package/docs-search-tool.js +28 -5
  37. package/docs-search-tool.js.map +1 -1
  38. package/docs-search-tool.mjs +28 -5
  39. package/docs-search-tool.mjs.map +1 -1
  40. package/http.d.mts +6 -3
  41. package/http.d.mts.map +1 -1
  42. package/http.d.ts +6 -3
  43. package/http.d.ts.map +1 -1
  44. package/http.js +70 -30
  45. package/http.js.map +1 -1
  46. package/http.mjs +70 -30
  47. package/http.mjs.map +1 -1
  48. package/index.js +17 -12
  49. package/index.js.map +1 -1
  50. package/index.mjs +17 -12
  51. package/index.mjs.map +1 -1
  52. package/instructions.d.mts +2 -0
  53. package/instructions.d.mts.map +1 -0
  54. package/instructions.d.ts +2 -0
  55. package/instructions.d.ts.map +1 -0
  56. package/instructions.js +46 -0
  57. package/instructions.js.map +1 -0
  58. package/instructions.mjs +43 -0
  59. package/instructions.mjs.map +1 -0
  60. package/logger.d.mts +7 -0
  61. package/logger.d.mts.map +1 -0
  62. package/logger.d.ts +7 -0
  63. package/logger.d.ts.map +1 -0
  64. package/logger.js +29 -0
  65. package/logger.js.map +1 -0
  66. package/logger.mjs +22 -0
  67. package/logger.mjs.map +1 -0
  68. package/methods.d.mts +10 -0
  69. package/methods.d.mts.map +1 -0
  70. package/methods.d.ts +10 -0
  71. package/methods.d.ts.map +1 -0
  72. package/methods.js +767 -0
  73. package/methods.js.map +1 -0
  74. package/methods.mjs +763 -0
  75. package/methods.mjs.map +1 -0
  76. package/options.d.mts +9 -0
  77. package/options.d.mts.map +1 -1
  78. package/options.d.ts +9 -0
  79. package/options.d.ts.map +1 -1
  80. package/options.js +62 -12
  81. package/options.js.map +1 -1
  82. package/options.mjs +62 -12
  83. package/options.mjs.map +1 -1
  84. package/package.json +78 -15
  85. package/server.d.mts +9 -10
  86. package/server.d.mts.map +1 -1
  87. package/server.d.ts +9 -10
  88. package/server.d.ts.map +1 -1
  89. package/server.js +79 -42
  90. package/server.js.map +1 -1
  91. package/server.mjs +78 -39
  92. package/server.mjs.map +1 -1
  93. package/src/{headers.ts → auth.ts} +18 -1
  94. package/src/code-tool-paths.cts +3 -0
  95. package/src/code-tool-types.ts +1 -0
  96. package/src/code-tool-worker.ts +387 -0
  97. package/src/code-tool.ts +326 -49
  98. package/src/docs-search-tool.ts +48 -7
  99. package/src/http.ts +86 -33
  100. package/src/index.ts +19 -13
  101. package/src/instructions.ts +65 -0
  102. package/src/logger.ts +28 -0
  103. package/src/methods.ts +788 -0
  104. package/src/options.ts +81 -12
  105. package/src/server.ts +90 -49
  106. package/src/stdio.ts +6 -4
  107. package/src/types.ts +12 -4
  108. package/src/util.ts +25 -0
  109. package/stdio.d.mts +2 -1
  110. package/stdio.d.mts.map +1 -1
  111. package/stdio.d.ts +2 -1
  112. package/stdio.d.ts.map +1 -1
  113. package/stdio.js +5 -4
  114. package/stdio.js.map +1 -1
  115. package/stdio.mjs +5 -4
  116. package/stdio.mjs.map +1 -1
  117. package/types.d.mts +8 -1
  118. package/types.d.mts.map +1 -1
  119. package/types.d.ts +8 -1
  120. package/types.d.ts.map +1 -1
  121. package/types.js.map +1 -1
  122. package/types.mjs.map +1 -1
  123. package/util.d.mts +4 -0
  124. package/util.d.mts.map +1 -0
  125. package/util.d.ts +4 -0
  126. package/util.d.ts.map +1 -0
  127. package/util.js +30 -0
  128. package/util.js.map +1 -0
  129. package/util.mjs +24 -0
  130. package/util.mjs.map +1 -0
  131. package/headers.d.mts +0 -4
  132. package/headers.d.mts.map +0 -1
  133. package/headers.d.ts +0 -4
  134. package/headers.d.ts.map +0 -1
  135. package/headers.js.map +0 -1
  136. package/headers.mjs +0 -18
  137. package/headers.mjs.map +0 -1
package/src/index.ts CHANGED
@@ -5,30 +5,39 @@ import { McpOptions, parseCLIOptions } from './options';
5
5
  import { launchStdioServer } from './stdio';
6
6
  import { launchStreamableHTTPServer } from './http';
7
7
  import type { McpTool } from './types';
8
+ import { configureLogger, getLogger } from './logger';
8
9
 
9
10
  async function main() {
10
11
  const options = parseOptionsOrError();
12
+ configureLogger({
13
+ level: options.debug ? 'debug' : 'info',
14
+ pretty: options.logFormat === 'pretty',
15
+ });
11
16
 
12
17
  const selectedTools = await selectToolsOrError(options);
13
18
 
14
- console.error(
15
- `MCP Server starting with ${selectedTools.length} tools:`,
16
- selectedTools.map((e) => e.tool.name),
19
+ getLogger().info(
20
+ { tools: selectedTools.map((e) => e.tool.name) },
21
+ `MCP Server starting with ${selectedTools.length} tools`,
17
22
  );
18
23
 
19
24
  switch (options.transport) {
20
25
  case 'stdio':
21
- await launchStdioServer();
26
+ await launchStdioServer(options);
22
27
  break;
23
28
  case 'http':
24
- await launchStreamableHTTPServer(options, options.port ?? options.socket);
29
+ await launchStreamableHTTPServer({
30
+ mcpOptions: options,
31
+ port: options.socket ?? options.port,
32
+ });
25
33
  break;
26
34
  }
27
35
  }
28
36
 
29
37
  if (require.main === module) {
30
38
  main().catch((error) => {
31
- console.error('Fatal error in main():', error);
39
+ // Logger might not be initialized yet
40
+ console.error('Fatal error in main()', error);
32
41
  process.exit(1);
33
42
  });
34
43
  }
@@ -37,7 +46,8 @@ function parseOptionsOrError() {
37
46
  try {
38
47
  return parseCLIOptions();
39
48
  } catch (error) {
40
- console.error('Error parsing options:', error);
49
+ // Logger is initialized after options, so use console.error here
50
+ console.error('Error parsing options', error);
41
51
  process.exit(1);
42
52
  }
43
53
  }
@@ -46,16 +56,12 @@ async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
46
56
  try {
47
57
  const includedTools = selectTools(options);
48
58
  if (includedTools.length === 0) {
49
- console.error('No tools match the provided filters.');
59
+ getLogger().error('No tools match the provided filters');
50
60
  process.exit(1);
51
61
  }
52
62
  return includedTools;
53
63
  } catch (error) {
54
- if (error instanceof Error) {
55
- console.error('Error filtering tools:', error.message);
56
- } else {
57
- console.error('Error filtering tools:', error);
58
- }
64
+ getLogger().error({ error }, 'Error filtering tools');
59
65
  process.exit(1);
60
66
  }
61
67
  }
@@ -0,0 +1,65 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { readEnv } from './util';
4
+ import { getLogger } from './logger';
5
+
6
+ const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
7
+
8
+ interface InstructionsCacheEntry {
9
+ fetchedInstructions: string;
10
+ fetchedAt: number;
11
+ }
12
+
13
+ const instructionsCache = new Map<string, InstructionsCacheEntry>();
14
+
15
+ // Periodically evict stale entries so the cache doesn't grow unboundedly.
16
+ const _cacheCleanupInterval = setInterval(() => {
17
+ const now = Date.now();
18
+ for (const [key, entry] of instructionsCache) {
19
+ if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
20
+ instructionsCache.delete(key);
21
+ }
22
+ }
23
+ }, INSTRUCTIONS_CACHE_TTL_MS);
24
+
25
+ // Don't keep the process alive just for cleanup.
26
+ _cacheCleanupInterval.unref();
27
+
28
+ export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> {
29
+ const cacheKey = stainlessApiKey ?? '';
30
+ const cached = instructionsCache.get(cacheKey);
31
+
32
+ if (cached && Date.now() - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
33
+ return cached.fetchedInstructions;
34
+ }
35
+
36
+ const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey);
37
+ instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: Date.now() });
38
+ return fetchedInstructions;
39
+ }
40
+
41
+ async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
42
+ // Setting the stainless API key is optional, but may be required
43
+ // to authenticate requests to the Stainless API.
44
+ const response = await fetch(
45
+ readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/metronome',
46
+ {
47
+ method: 'GET',
48
+ headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
49
+ },
50
+ );
51
+
52
+ let instructions: string | undefined;
53
+ if (!response.ok) {
54
+ getLogger().warn(
55
+ 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
56
+ );
57
+
58
+ instructions =
59
+ '\n This is the metronome MCP server.\n\n Available tools:\n - search_docs: Search SDK documentation to find the right methods and parameters.\n - execute: Run TypeScript code against a pre-authenticated SDK client. Define an async run(client) function.\n\n Workflow:\n - If unsure about the API, call search_docs first.\n - Write complete solutions in a single execute call when possible. For large datasets, use API filters to narrow results or paginate within a single execute block.\n - If execute returns an error, read the error and fix your code rather than retrying the same approach.\n - Variables do not persist between execute calls. Return or log all data you need.\n - Individual HTTP requests to the API have a 30-second timeout. If a request times out, try a smaller query or add filters.\n - Code execution has a total timeout of approximately 5 minutes. If your code times out, simplify it or break it into smaller steps.\n ';
60
+ }
61
+
62
+ instructions ??= ((await response.json()) as { instructions: string }).instructions;
63
+
64
+ return instructions;
65
+ }
package/src/logger.ts ADDED
@@ -0,0 +1,28 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { pino, type Level, type Logger } from 'pino';
4
+ import pretty from 'pino-pretty';
5
+
6
+ let _logger: Logger | undefined;
7
+
8
+ export function configureLogger({ level, pretty: usePretty }: { level: Level; pretty: boolean }): void {
9
+ _logger = pino(
10
+ {
11
+ level,
12
+ timestamp: pino.stdTimeFunctions.isoTime,
13
+ formatters: {
14
+ level(label) {
15
+ return { level: label };
16
+ },
17
+ },
18
+ },
19
+ usePretty ? pretty({ colorize: true, levelFirst: true, destination: 2 }) : process.stderr,
20
+ );
21
+ }
22
+
23
+ export function getLogger(): Logger {
24
+ if (!_logger) {
25
+ throw new Error('Logger has not been configured. Call configureLogger() before using the logger.');
26
+ }
27
+ return _logger;
28
+ }