Skip to content

Commit 902cc44

Browse files
chore(internal): make generated MCP servers compatible with Cloudflare worker environments
1 parent 588523b commit 902cc44

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
export const workerPath = require.resolve('./code-tool-worker.mjs');
3+
export function getWorkerPath(): string {
4+
return require.resolve('./code-tool-worker.mjs');
5+
}

packages/mcp-server/src/code-tool.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
import fs from 'node:fs';
4-
import path from 'node:path';
5-
import url from 'node:url';
6-
import { newDenoHTTPWorker } from '@valtown/deno-http-worker';
7-
import { workerPath } from './code-tool-paths.cjs';
83
import {
94
ContentBlock,
105
McpRequestContext,
@@ -212,6 +207,13 @@ const localDenoHandler = async ({
212207
reqContext: McpRequestContext;
213208
args: unknown;
214209
}): Promise<ToolCallResult> => {
210+
const fs = await import('node:fs');
211+
const path = await import('node:path');
212+
const url = await import('node:url');
213+
const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
214+
const { getWorkerPath } = await import('./code-tool-paths.cjs');
215+
const workerPath = getWorkerPath();
216+
215217
const client = reqContext.client;
216218
const baseURLHostname = new URL(client.baseURL).hostname;
217219
const { code } = args as { code: string };

packages/mcp-server/src/instructions.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,24 @@ interface InstructionsCacheEntry {
1212

1313
const instructionsCache = new Map<string, InstructionsCacheEntry>();
1414

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-
2815
export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> {
16+
const now = Date.now();
2917
const cacheKey = stainlessApiKey ?? '';
3018
const cached = instructionsCache.get(cacheKey);
3119

32-
if (cached && Date.now() - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
20+
if (cached && now - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
3321
return cached.fetchedInstructions;
3422
}
3523

24+
// Evict stale entries so the cache doesn't grow unboundedly.
25+
for (const [key, entry] of instructionsCache) {
26+
if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
27+
instructionsCache.delete(key);
28+
}
29+
}
30+
3631
const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey);
37-
instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: Date.now() });
32+
instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: now });
3833
return fetchedInstructions;
3934
}
4035

0 commit comments

Comments
 (0)