perf: add preloadSchemas() and warm wire schemas eagerly on workerd (#2483)
This commit is contained in:
parent
8e1d2e92b1
commit
3f07a325c6
3 changed files with 550 additions and 14 deletions
|
|
@ -99,5 +99,11 @@ export type { CacheHint, CacheScope } from '@modelcontextprotocol/core-internal'
|
|||
export type { ElicitInputParams, InputRequiredSpec, InputResponseView } from '@modelcontextprotocol/core-internal';
|
||||
export { acceptedContent, inputRequired, inputResponse } from '@modelcontextprotocol/core-internal';
|
||||
|
||||
// Explicit opt-in to eager wire-schema construction, for platforms that bill
|
||||
// request CPU but not module evaluation (isolate-based edge/serverless
|
||||
// runtimes). The package's workerd build calls it automatically at module
|
||||
// scope; other builds stay lazy unless the application calls it itself.
|
||||
export { preloadSchemas } from '@modelcontextprotocol/core-internal';
|
||||
|
||||
// re-export curated public API from core
|
||||
export * from '@modelcontextprotocol/core-internal/public';
|
||||
|
|
|
|||
27
packages/server/src/shimsBrowser.ts
Normal file
27
packages/server/src/shimsBrowser.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Browser runtime shims for server package
|
||||
*
|
||||
* This file is selected via package.json export conditions when bundling for
|
||||
* browsers. It binds the same platform choices as the workerd shim (the
|
||||
* cfWorker validator, the process stub) WITHOUT the module-scope
|
||||
* `preloadSchemas()` call: in a browser, module evaluation is page load —
|
||||
* boot latency — so schema construction stays lazy, exactly like Node.
|
||||
*/
|
||||
export { CfWorkerJsonSchemaValidator as DefaultJsonSchemaValidator } from '@modelcontextprotocol/core-internal/validators/cfWorker';
|
||||
|
||||
/**
|
||||
* Stub process object for non-Node.js environments.
|
||||
* StdioServerTransport is not supported in Cloudflare Workers/browser environments.
|
||||
*/
|
||||
function notSupported(): never {
|
||||
throw new Error('StdioServerTransport is not supported in this environment. Use StreamableHTTPServerTransport instead.');
|
||||
}
|
||||
|
||||
export const process = {
|
||||
get stdin(): never {
|
||||
return notSupported();
|
||||
},
|
||||
get stdout(): never {
|
||||
return notSupported();
|
||||
}
|
||||
};
|
||||
|
|
@ -3,8 +3,17 @@
|
|||
*
|
||||
* This file is selected via package.json export conditions when running in workerd.
|
||||
*/
|
||||
import { preloadSchemas } from '@modelcontextprotocol/core-internal';
|
||||
|
||||
export { CfWorkerJsonSchemaValidator as DefaultJsonSchemaValidator } from '@modelcontextprotocol/core-internal/validators/cfWorker';
|
||||
|
||||
// Platform asymmetry: isolate platforms like workerd evaluate module scope
|
||||
// during deployment/isolate warm-up, outside any request's billed CPU, while
|
||||
// lazy construction would land inside the first request each fresh isolate
|
||||
// serves. The Node and browser shims stay lazy — there, module evaluation is
|
||||
// process/page startup and boot latency is the cost that matters.
|
||||
preloadSchemas();
|
||||
|
||||
/**
|
||||
* Stub process object for non-Node.js environments.
|
||||
* StdioServerTransport is not supported in Cloudflare Workers/browser environments.
|
||||
|
|
|
|||
Loading…
Reference in a new issue