inspector/test-servers/src/composable-test-server.ts
Cliff Hall 0ead1deea6
feat(core): port v1.5 InspectorClient + transports + auth subsystem (#1302) (#1306)
* wip(core): bulk-copy v1.5 InspectorClient runtime files (#1302)

Verbatim ports from origin/v1.5/main into v2/main core/:

- logging/ (2 files)
- storage/ + storage/adapters/ (4 files)
- auth/ + auth/browser/ + auth/node/ + auth/remote/ (16 files)
- mcp/config.ts + mcp/node/ (4 files, stdio transport)
- mcp/remote/ + mcp/remote/node/ (12 files, HTTP/SSE transport)
- mcp/inspectorClient.ts (~2169 LOC), messageTrackingTransport,
  fetchTracking, oauthManager, index, state/index

Total: ~7,800 LOC of source files, copied unmodified via
`git show origin/v1.5/main:<path> > <dest>`.

Also:
- Add deps: pino, hono, @hono/node-server, atomically, zustand, @types/node
- tsconfig.app.json: extend paths to resolve new node-only deps from
  core/, add "node" to types, disable erasableSyntaxOnly (v1.5 code
  uses parameter properties — TODO to re-enable in follow-up)

Compiles down to 47 type errors, all in three categories:
- 24 TS2305: v2 dropped v1.5 type exports (InspectorClientOptions,
  CreateTransport, InspectorClientEnvironment, AppRendererClient,
  SamplingCreateMessage class, ElicitationCreateMessage class,
  jsonUtils helpers, OAuth events). Per user direction, will re-add
  these to v2's diverged files (types.ts, samplingCreateMessage.ts,
  etc.) so the ported code stays verbatim.
- 15 TS7006: implicit-any params in v1.5 code (v1.5 tsconfig was
  looser than v2's noImplicitAny). Need explicit annotations.
- 8 other TS errors: ../package.json import, Transport|null narrowing,
  hono Context generic mismatch, one unused parameter.

Tests not yet ported (19 files, ~11,170 LOC).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* wip(core): integrate ported v1.5 InspectorClient + port tests (#1302)

Source integration (typecheck + build now pass):
- Re-add v1.5 types/classes dropped during v2 evolution:
  - types.ts: CreateTransport*, AppRendererClient, InspectorClient{Environment,Options}
  - samplingCreateMessage.ts / elicitationCreateMessage.ts: classes + keep v2's
    InspectorPending{Sampling,Elicitation} interface
  - inspectorClientEventTarget.ts: oauth* event keys
  - json/jsonUtils.ts: convertParameterValue / convertToolParameters /
    convertPromptArguments + ParameterSchema
- inspectorClient.ts: replace `../package.json` import (v2 has no
  core/package.json) with a hardcoded identity constant
- remote/node/server.ts: cast hono Context for streamSSE (hono's
  generic typing tightened since v1.5)
- oauth-callback-server.ts: drop unused hostname field
- tsconfig: enable @types/node, add paths for new bare-module deps
  (pino, zustand, hono, atomically) in app + test configs
- vite.config.ts: mirror those module aliases for the unit project
  (which runs from repoRoot)

Tests ported (19 files, ~11K LOC) under clients/web/src/test/core/:
- All core/__tests__/auth/* (7 files)
- inspectorClient.test.ts + 4 OAuth variants
- mcp/oauthManager.test.ts, jsonUtils.test.ts
- transport, remote-transport, remote-server-config, storage-adapters
- helpers/oauth-client-fixtures.ts
Bulk-rewrote `from "../mcp/…"` → `from "@inspector/core/mcp/…"` etc.

Current state:
- typecheck: 0 errors
- build: passes (vite v8, 788 modules)
- unit tests: 1014 / 1034 pass (937 pre-existing + 77 new core tests)
- 20 failures in 13 files; failure modes:
  - 13 file-load failures needing the v1.5 `test-servers/` package
    (@modelcontextprotocol/inspector-test-server) which isn't in v2 yet
    or real fs/network not available in happy-dom
  - 7 assertion failures in discovery.test.ts + state-machine.test.ts
    (SDK 1.29 vs 1.25 API drift — mocks don't match new shape)

Coverage gate not yet checked against new files.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* wip(core): port test-servers, more types, partial test integration (#1302)

- Port v1.5 test-servers/ package (12 files, ~5K LOC) under
  repo-root/test-servers/, wire @modelcontextprotocol/inspector-test-server
  alias in both vite.config.ts (regex form) and tsconfig.test.json.
- Add deps: express ^5, yaml ^2, @types/express ^5.
- Re-export InspectorClientOptions/Environment/Transport* from inspectorClient.ts
  so v1.5 tests that import them from that module path keep resolving.
- Switch InspectorClientEventMap to use SamplingCreateMessage /
  ElicitationCreateMessage classes (matches v1.5 event payload contracts)
  while keeping the InspectorPending{Sampling,Elicitation} interfaces in
  the *.ts modules for v2 consumers that still use them.
- Fix verbatim-module-syntax issue in test-servers and drop unused stdio
  config field.
- Adjust inspectorClient.test.ts task types to TaskWithOptionalCreatedAt
  (SDK 1.29 tightened Task shape vs v1.5's 1.25).
- Drop spurious maxMessages/maxFetchRequests/maxStderrLogEvents from
  remote-transport.test.ts (never on InspectorClientOptions).

State:
- typecheck: 0 errors
- build: passes
- unit tests: 1099 / 1270 pass
- 171 failures in 10 test files, almost all integration tests that need
  real HTTP/fs/network (happy-dom can't host them). Next: add a node-env
  vitest project to run these properly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(core): port v1.5 InspectorClient + transports + auth subsystem (#1302)

Bulk-port of the v1.5 core runtime (~7,800 LOC source + ~16K LOC tests +
~5K LOC test-servers/) into v2. Files copied verbatim from origin/v1.5/main
via `git show >`; only minimal adapters added on the v2 side.

Subsystems landed
-----------------
- core/logging/, core/storage/, core/auth/ (browser + node + remote)
- core/mcp/ runtime: inspectorClient.ts (2169 LOC), oauthManager,
  messageTrackingTransport, fetchTracking, config, index
- core/mcp/node/ (stdio transport)
- core/mcp/remote/ + remote/node/ (HTTP/SSE transport + Hono server)
- test-servers/ package (composable MCP test servers, fixtures, harness)

v2 adapter layer
----------------
- types.ts: append CreateTransport*, AppRendererClient,
  InspectorClient{Environment,Options} (v2 had pruned these — v1.5 needs them)
- samplingCreateMessage / elicitationCreateMessage: keep v1.5 classes AND
  v2's Inspector Pending* interfaces in the same files
- inspectorClientEventTarget: add oauth* event keys; switch pending
  sampling/elicitation payloads to the classes (matches v1.5 contracts)
- json/jsonUtils: append convertParameterValue/convertToolParameters/
  convertPromptArguments + ParameterSchema
- inspectorClient.ts: re-export Options/Environment/Transport* so tests
  that import these from `inspectorClient.js` keep resolving; replace
  `../package.json` import with a hardcoded identity (v2 has no core/package.json)
- remote/node/server.ts: cast hono Context for streamSSE (hono generic
  tightened since v1.5)
- oauth-callback-server: drop unused hostname field
- tsconfig.app.json / tsconfig.test.json: add @types/node + paths for
  pino/zustand/hono/@hono/node-server/atomically/express/yaml, disable
  erasableSyntaxOnly (parameter-property syntax from v1.5)
- vite.config.ts: regex-form module aliases (so the package's exports
  field handles subpaths like `hono/streaming`, `zustand/middleware`),
  alias for @modelcontextprotocol/inspector-test-server → test-servers/src

Tests
-----
- 19 v1.5 test files copied under clients/web/src/test/core/,
  imports rewired to @inspector/core/* and @modelcontextprotocol/inspector-test-server
- 1004 unit tests pass; 298 storybook tests pass; coverage gate passes

Known follow-ups (out-of-scope here, tracked in TODOs)
------------------------------------------------------
- 13 integration test files are excluded from the unit project (spawn real
  HTTP/stdio servers; run real OAuth flows; mock SDK auth → happy-dom CORS).
  Needs a node-env vitest project. Files listed under `test.projects[0].test.exclude`.
- Source files exercised only by those integration tests are excluded from
  the coverage gate pending the node-env project. Listed under
  `test.coverage.exclude` with comment.
- Manual smoke tests against real stdio / HTTP / OAuth server per the AC
  on #1302 still need verification locally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs/fix: address #1306 review (AGENTS tree, #1307 links, @types/node, .substr)

- AGENTS.md: expand the project-structure tree to include all v1.5-ported
  subsystems (core/auth/{browser,node,remote}, core/storage, core/logging,
  core/mcp/{node,remote/{,node}}, core/mcp/state) plus the new top-level
  test-servers/ directory (with a note about its @modelcontextprotocol/
  inspector-test-server alias).
- vite.config.ts: reference #1307 from both the test exclude block and the
  coverage exclude block so the next person editing either sees the
  marching order for shrinking the lists.
- package.json: restore @types/node to ^24 (an earlier `npm install
  --save-dev @types/node@^22` accidentally pinned it lower than the repo
  was using; nothing here required ^22).
- core/mcp/fetchTracking.ts: replace deprecated .substr(2, 9) with
  .slice(2, 11) to match the convention in core/mcp/inspectorClient.ts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 15:36:17 -04:00

959 lines
32 KiB
TypeScript

/**
* Composable Test Server
*
* Provides types and functions for creating MCP test servers from configuration.
* This allows composing MCP test servers with different capabilities, tools, resources, and prompts.
*/
import {
McpServer,
ResourceTemplate as SdkResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import type {
Implementation,
Tool,
Resource,
ResourceTemplate,
Prompt,
CallToolResult,
} from "@modelcontextprotocol/sdk/types.js";
import {
InMemoryTaskStore,
InMemoryTaskMessageQueue,
} from "@modelcontextprotocol/sdk/experimental/tasks/stores/in-memory.js";
import type {
TaskStore,
TaskMessageQueue,
ToolTaskHandler,
} from "@modelcontextprotocol/sdk/experimental/tasks/interfaces.js";
import type {
RegisteredTool,
RegisteredResource,
RegisteredPrompt,
RegisteredResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
import type {
ServerRequest,
ServerNotification,
} from "@modelcontextprotocol/sdk/types.js";
import {
SetLevelRequestSchema,
SubscribeRequestSchema,
UnsubscribeRequestSchema,
ListToolsRequestSchema,
ListResourcesRequestSchema,
ListResourceTemplatesRequestSchema,
ListPromptsRequestSchema,
type ListToolsResult,
type ListResourcesResult,
type ListResourceTemplatesResult,
type ListPromptsResult,
} from "@modelcontextprotocol/sdk/types.js";
import type { AnySchema } from "@modelcontextprotocol/sdk/server/zod-compat.js";
import {
type ZodRawShapeCompat,
getObjectShape,
getSchemaDescription,
isSchemaOptional,
normalizeObjectSchema,
} from "@modelcontextprotocol/sdk/server/zod-compat.js";
import { toJsonSchemaCompat } from "@modelcontextprotocol/sdk/server/zod-json-schema-compat.js";
import {
completable,
isCompletable,
} from "@modelcontextprotocol/sdk/server/completable.js";
import type { PromptArgument } from "@modelcontextprotocol/sdk/types.js";
// Empty object JSON schema constant (from SDK's mcp.js)
const EMPTY_OBJECT_JSON_SCHEMA = {
type: "object",
properties: {},
} as const;
type ToolInputSchema = ZodRawShapeCompat;
type PromptArgsSchema = ZodRawShapeCompat;
interface ServerState {
registeredTools: Map<string, RegisteredTool>; // Keyed by name
registeredResources: Map<string, RegisteredResource>; // Keyed by URI
registeredPrompts: Map<string, RegisteredPrompt>; // Keyed by name
registeredResourceTemplates: Map<string, RegisteredResourceTemplate>; // Keyed by uriTemplate
listChangedConfig: {
tools?: boolean;
resources?: boolean;
prompts?: boolean;
};
resourceSubscriptions: Set<string>; // Set of subscribed resource URIs
}
/**
* Context object passed to tool handlers containing both server and state
*/
export interface TestServerContext {
server: McpServer;
state: ServerState;
serverControl?: { isClosing(): boolean };
}
export interface ToolDefinition {
name: string;
description: string;
inputSchema?: ToolInputSchema;
/** Optional Zod object schema for tool output; when set, handler must return structuredContent. */
outputSchema?: unknown;
handler: (
params: Record<string, unknown>,
context?: TestServerContext,
extra?: RequestHandlerExtra<ServerRequest, ServerNotification>,
) => Promise<CallToolResult>;
}
export interface TaskToolDefinition {
name: string;
description: string;
inputSchema?: ToolInputSchema;
execution?: { taskSupport: "required" | "optional" };
handler: ToolTaskHandler<ToolInputSchema | undefined>;
}
export interface ResourceDefinition {
uri: string;
name: string;
description?: string;
mimeType?: string;
text?: string;
}
export interface PromptDefinition {
name: string;
description?: string;
promptString: string; // The prompt text with optional {argName} placeholders
argsSchema?: PromptArgsSchema; // Can include completable() schemas
// Optional completion callbacks keyed by argument name
// This is a convenience - users can also use completable() directly in argsSchema
completions?: Record<
string,
(
argumentValue: string,
context?: Record<string, string>,
) => Promise<string[]> | string[]
>;
}
export interface ResourceTemplateDefinition {
name: string;
uriTemplate: string; // URI template with {variable} placeholders (RFC 6570)
description?: string;
inputSchema?: ZodRawShapeCompat; // Schema for template variables
handler: (
uri: URL,
params: Record<string, unknown>,
context?: TestServerContext,
extra?: RequestHandlerExtra<ServerRequest, ServerNotification>,
) => Promise<{
contents: Array<{ uri: string; mimeType?: string; text: string }>;
}>;
// Optional callbacks for resource template operations
// list: Can return either:
// - string[] (convenience - will be converted to ListResourcesResult with uri and name)
// - ListResourcesResult (full control - includes uri, name, description, mimeType, etc.)
list?:
| (() => Promise<string[]> | string[])
| (() => Promise<ListResourcesResult> | ListResourcesResult);
// complete: Map of variable names to completion callbacks
// OR a single callback function that will be used for all variables
complete?:
| Record<
string,
(
value: string,
context?: Record<string, string>,
) => Promise<string[]> | string[]
>
| ((
argumentName: string,
argumentValue: string,
context?: Record<string, string>,
) => Promise<string[]> | string[]);
}
/**
* Configuration for composing an MCP server
*/
export interface ServerConfig {
serverInfo: Implementation; // Server metadata (name, version, etc.) - required
tools?: (ToolDefinition | TaskToolDefinition)[]; // Tools to register (optional, empty array means no tools, but tools capability is still advertised)
resources?: ResourceDefinition[]; // Resources to register (optional, empty array means no resources, but resources capability is still advertised)
resourceTemplates?: ResourceTemplateDefinition[]; // Resource templates to register (optional, empty array means no templates, but resources capability is still advertised)
prompts?: PromptDefinition[]; // Prompts to register (optional, empty array means no prompts, but prompts capability is still advertised)
logging?: boolean; // Whether to advertise logging capability (default: false)
onLogLevelSet?: (level: string) => void; // Optional callback when log level is set (for testing)
onRegisterResource?: (resource: ResourceDefinition) =>
| (() => Promise<{
contents: Array<{ uri: string; mimeType?: string; text: string }>;
}>)
| undefined; // Optional callback to customize resource handler during registration
serverType?: "sse" | "streamable-http"; // Transport type (default: "streamable-http")
port?: number; // Port to use (optional, will find available port if not specified)
/**
* Whether to advertise listChanged capability for each list type
* If enabled, modification tools will send list_changed notifications
*/
listChanged?: {
tools?: boolean; // default: false
resources?: boolean; // default: false
prompts?: boolean; // default: false
};
/**
* Whether to advertise resource subscriptions capability
* If enabled, server will advertise resources.subscribe capability
*/
subscriptions?: boolean; // default: false
/**
* Maximum page size for pagination (optional, undefined means no pagination)
* When set, custom list handlers will paginate results using this page size
*/
maxPageSize?: {
tools?: number;
resources?: number;
resourceTemplates?: number;
prompts?: number;
};
/**
* Whether to advertise tasks capability
* If enabled, server will advertise tasks capability with list and cancel support
*/
tasks?: {
list?: boolean; // default: true
cancel?: boolean; // default: true
};
/**
* Task store implementation (optional, defaults to InMemoryTaskStore)
* Only used if tasks capability is enabled
*/
taskStore?: TaskStore;
/**
* Task message queue implementation (optional, defaults to InMemoryTaskMessageQueue)
* Only used if tasks capability is enabled
*/
taskMessageQueue?: TaskMessageQueue;
/**
* OAuth 2.1 configuration for test server
* If enabled, server will act as an OAuth authorization server
*/
oauth?: {
/**
* Whether OAuth is enabled for this test server
*/
enabled: boolean;
/**
* OAuth authorization server issuer URL
* Used for metadata endpoints and token issuance
* If not provided, defaults to the test server's base URL
*/
issuerUrl?: URL;
/**
* List of scopes supported by this authorization server
* Defaults to ["mcp"] if not provided
*/
scopesSupported?: string[];
/**
* If true, MCP endpoints require valid Bearer token
* Returns 401 Unauthorized if token is missing or invalid
*/
requireAuth?: boolean;
/**
* Static/preregistered clients for testing
* These clients are pre-configured and don't require DCR
*/
staticClients?: Array<{
clientId: string;
clientSecret?: string;
redirectUris?: string[];
}>;
/**
* Whether to support Dynamic Client Registration (DCR)
* If true, exposes /register endpoint for client registration
*/
supportDCR?: boolean;
/**
* Whether to support CIMD (Client ID Metadata Documents)
* If true, server will fetch client metadata from clientMetadataUrl
*/
supportCIMD?: boolean;
/**
* Token expiration time in seconds (default: 3600)
*/
tokenExpirationSeconds?: number;
/**
* Whether to support refresh tokens (default: true)
*/
supportRefreshTokens?: boolean;
};
/**
* Optional server control for orderly shutdown (test HTTP server).
* When present, progress-sending tools check isClosing() before sending and skip/break if closing.
*/
serverControl?: { isClosing(): boolean };
}
/**
* Create and configure an McpServer instance from ServerConfig
* This centralizes the setup logic shared between HTTP and stdio test servers
*/
export function createMcpServer(config: ServerConfig): McpServer {
// Build capabilities based on config
const capabilities: {
tools?: object;
resources?: { subscribe?: boolean };
prompts?: object;
logging?: object;
tasks?: {
list?: object;
cancel?: object;
requests?: { tools?: { call?: object } };
};
} = {};
if (config.tools !== undefined) {
capabilities.tools = {};
}
if (
config.resources !== undefined ||
config.resourceTemplates !== undefined
) {
capabilities.resources = {};
// Add subscribe capability if subscriptions are enabled
if (config.subscriptions === true) {
capabilities.resources.subscribe = true;
}
}
if (config.prompts !== undefined) {
capabilities.prompts = {};
}
if (config.logging === true) {
capabilities.logging = {};
}
if (config.tasks !== undefined) {
capabilities.tasks = {
list: config.tasks.list !== false ? {} : undefined,
cancel: config.tasks.cancel !== false ? {} : undefined,
requests: { tools: { call: {} } },
};
// Remove undefined values
if (capabilities.tasks.list === undefined) {
delete capabilities.tasks.list;
}
if (capabilities.tasks.cancel === undefined) {
delete capabilities.tasks.cancel;
}
}
// Create task store and message queue if tasks are enabled
const taskStore =
config.tasks !== undefined
? config.taskStore || new InMemoryTaskStore()
: undefined;
const taskMessageQueue =
config.tasks !== undefined
? config.taskMessageQueue || new InMemoryTaskMessageQueue()
: undefined;
// Create the server with capabilities and task stores
const mcpServer = new McpServer(config.serverInfo, {
capabilities,
taskStore,
taskMessageQueue,
});
// Create state (this is really session state, which is what we'll call it if we implement sessions at some point)
const state: ServerState = {
registeredTools: new Map(), // Keyed by name
registeredResources: new Map(), // Keyed by URI
registeredPrompts: new Map(), // Keyed by name
registeredResourceTemplates: new Map(), // Keyed by uriTemplate
listChangedConfig: config.listChanged || {},
resourceSubscriptions: new Set<string>(), // Track subscribed resource URIs
};
// Create context object
const context: TestServerContext = {
server: mcpServer,
state,
...(config.serverControl && { serverControl: config.serverControl }),
};
// Set up logging handler if logging is enabled
if (config.logging === true) {
mcpServer.server.setRequestHandler(
SetLevelRequestSchema,
async (request) => {
// Call optional callback if provided (for testing)
if (config.onLogLevelSet) {
config.onLogLevelSet(request.params.level);
}
// Return empty result as per MCP spec
return {};
},
);
}
// Set up resource subscription handlers if subscriptions are enabled
if (config.subscriptions === true) {
mcpServer.server.setRequestHandler(
SubscribeRequestSchema,
async (request) => {
// Track subscription in state (accessible via closure)
const uri = request.params.uri;
state.resourceSubscriptions.add(uri);
return {};
},
);
mcpServer.server.setRequestHandler(
UnsubscribeRequestSchema,
async (request) => {
// Remove subscription from state (accessible via closure)
const uri = request.params.uri;
state.resourceSubscriptions.delete(uri);
return {};
},
);
}
// Type guard to check if a tool is a task tool
function isTaskTool(
tool: ToolDefinition | TaskToolDefinition,
): tool is TaskToolDefinition {
return (
"handler" in tool &&
typeof tool.handler === "object" &&
tool.handler !== null &&
"createTask" in tool.handler
);
}
// Set up tools
if (config.tools && config.tools.length > 0) {
for (const tool of config.tools) {
if (isTaskTool(tool)) {
// Register task-based tool
// registerToolTask has two overloads: one with inputSchema (required) and one without
const registered = tool.inputSchema
? mcpServer.experimental.tasks.registerToolTask(
tool.name,
{
description: tool.description,
inputSchema: tool.inputSchema,
execution: tool.execution,
},
tool.handler,
)
: mcpServer.experimental.tasks.registerToolTask(
tool.name,
{
description: tool.description,
execution: tool.execution,
},
tool.handler,
);
state.registeredTools.set(tool.name, registered);
} else {
// Register regular tool
const registered = mcpServer.registerTool(
tool.name,
{
description: tool.description,
inputSchema: tool.inputSchema,
...(tool.outputSchema != null && {
outputSchema: tool.outputSchema as AnySchema,
}),
},
async (args, extra) => {
const result = await tool.handler(
args as Record<string, unknown>,
context,
extra,
);
const rawStructured =
result &&
typeof result === "object" &&
"structuredContent" in result
? (result as { structuredContent?: unknown }).structuredContent
: undefined;
const structuredContent =
rawStructured !== undefined && rawStructured !== null
? (rawStructured as Record<string, unknown>)
: undefined;
// If handler returns content array, use it; otherwise build content from message or stringify
let content: Array<{ type: "text"; text: string }>;
if (result && Array.isArray(result.content)) {
content = result.content as Array<{ type: "text"; text: string }>;
} else if (result && typeof result.message === "string") {
content = [{ type: "text" as const, text: result.message }];
} else {
content = [
{
type: "text" as const,
text: JSON.stringify(result ?? {}),
},
];
}
return {
content,
...(structuredContent !== undefined && { structuredContent }),
};
},
);
state.registeredTools.set(tool.name, registered);
}
}
}
// Set up resources
if (config.resources && config.resources.length > 0) {
for (const resource of config.resources) {
// Check if there's a custom handler from the callback
const customHandler = config.onRegisterResource
? config.onRegisterResource(resource)
: undefined;
const registered = mcpServer.registerResource(
resource.name,
resource.uri,
{
description: resource.description,
mimeType: resource.mimeType,
},
customHandler ||
(async () => {
return {
contents: [
{
uri: resource.uri,
mimeType: resource.mimeType || "text/plain",
text: resource.text ?? "",
},
],
};
}),
);
state.registeredResources.set(resource.uri, registered);
}
}
// Set up resource templates
if (config.resourceTemplates && config.resourceTemplates.length > 0) {
for (const template of config.resourceTemplates) {
// ResourceTemplate is a class - create an instance with the URI template string and callbacks
// Convert list callback: SDK expects ListResourcesResult
// We support both string[] (convenience) and ListResourcesResult (full control)
const listCallback = template.list
? async () => {
const result = template.list!();
const resolved = await result;
// Check if it's already a ListResourcesResult (has resources array)
if (
resolved &&
typeof resolved === "object" &&
"resources" in resolved
) {
return resolved as ListResourcesResult;
}
// Otherwise, it's string[] - convert to ListResourcesResult
const uriArray = resolved as string[];
return {
resources: uriArray.map((uri) => ({
uri,
name: uri, // Use URI as name if not provided
})),
};
}
: undefined;
// Convert complete callback: SDK expects {[variable: string]: callback}
// We support either a map or a single function
let completeCallbacks:
| {
[variable: string]: (
value: string,
context?: { arguments?: Record<string, string> },
) => Promise<string[]> | string[];
}
| undefined = undefined;
if (template.complete) {
if (typeof template.complete === "function") {
// Single function - extract variable names from URI template and use for all
// Parse URI template to find variables (e.g., {file} from "file://{file}")
const variableMatches = template.uriTemplate.match(/\{([^}]+)\}/g);
if (variableMatches) {
completeCallbacks = {};
const completeFn = template.complete;
for (const match of variableMatches) {
const variableName = match.slice(1, -1); // Remove { and }
completeCallbacks[variableName] = async (
value: string,
context?: { arguments?: Record<string, string> },
) => {
const result = completeFn(
variableName,
value,
context?.arguments,
);
return Array.isArray(result) ? result : await result;
};
}
}
} else {
// Map of variable names to callbacks
completeCallbacks = {};
for (const [variableName, callback] of Object.entries(
template.complete,
)) {
completeCallbacks[variableName] = async (
value: string,
context?: { arguments?: Record<string, string> },
) => {
const result = callback(value, context?.arguments);
return Array.isArray(result) ? result : await result;
};
}
}
}
const resourceTemplate = new SdkResourceTemplate(template.uriTemplate, {
list: listCallback,
complete: completeCallbacks,
});
const registered = mcpServer.registerResource(
template.name,
resourceTemplate,
{
description: template.description,
},
async (uri: URL, variables: Record<string, unknown>, extra) => {
const result = await template.handler(uri, variables, context, extra);
return result;
},
);
state.registeredResourceTemplates.set(template.uriTemplate, registered);
}
}
// Set up prompts
if (config.prompts && config.prompts.length > 0) {
for (const prompt of config.prompts) {
// Build argsSchema with completion support if provided
let argsSchema = prompt.argsSchema;
// If completions callbacks are provided, wrap the corresponding schemas
if (prompt.completions && argsSchema) {
const enhancedSchema: ZodRawShapeCompat = { ...argsSchema };
for (const [argName, completeCallback] of Object.entries(
prompt.completions,
)) {
if (enhancedSchema[argName]) {
// Wrap with completable only if not already wrapped (avoids "Cannot redefine property" when createMcpServer is called multiple times with shared config)
if (!isCompletable(enhancedSchema[argName])) {
enhancedSchema[argName] = completable(
enhancedSchema[argName],
async (
value: unknown,
context?: { arguments?: Record<string, string> },
) => {
const result = completeCallback(
String(value),
context?.arguments,
);
return Array.isArray(result) ? result : await result;
},
);
}
}
}
argsSchema = enhancedSchema;
}
const registered = mcpServer.registerPrompt(
prompt.name,
{
description: prompt.description,
argsSchema: argsSchema,
},
async (args) => {
let text = prompt.promptString;
// If args are provided, substitute them into the prompt string
// Replace {argName} with the actual value
if (args && typeof args === "object") {
for (const [key, value] of Object.entries(args)) {
const placeholder = `{${key}}`;
text = text.replace(
new RegExp(placeholder.replace(/[{}]/g, "\\$&"), "g"),
String(value),
);
}
}
return {
messages: [
{
role: "user",
content: {
type: "text",
text,
},
},
],
};
},
);
state.registeredPrompts.set(prompt.name, registered);
}
}
// Set up pagination handlers if maxPageSize is configured
const maxPageSize = config.maxPageSize || {};
// Tools pagination
if (capabilities.tools && maxPageSize.tools !== undefined) {
mcpServer.server.setRequestHandler(
ListToolsRequestSchema,
async (request) => {
const cursor = request.params?.cursor;
const pageSize = maxPageSize.tools!;
// Convert registered tools to Tool format using the same logic as the SDK (mcp.js lines 67-95)
const allTools: Tool[] = [];
for (const [name, registered] of state.registeredTools.entries()) {
if (registered.enabled) {
// Match SDK's approach exactly (mcp.js lines 71-95)
const toolDefinition: Record<string, unknown> = {
name,
title: registered.title,
description: registered.description,
inputSchema: (() => {
const obj = normalizeObjectSchema(registered.inputSchema);
return obj
? toJsonSchemaCompat(obj, {
strictUnions: true,
pipeStrategy: "input",
})
: EMPTY_OBJECT_JSON_SCHEMA;
})(),
annotations: registered.annotations,
execution: registered.execution,
_meta: registered._meta,
};
if (registered.outputSchema) {
const obj = normalizeObjectSchema(registered.outputSchema);
if (obj) {
toolDefinition.outputSchema = toJsonSchemaCompat(obj, {
strictUnions: true,
pipeStrategy: "output",
});
}
}
allTools.push(toolDefinition as Tool);
}
}
const startIndex = cursor ? parseInt(cursor, 10) : 0;
const endIndex = startIndex + pageSize;
const page = allTools.slice(startIndex, endIndex);
const nextCursor =
endIndex < allTools.length ? endIndex.toString() : undefined;
return {
tools: page,
nextCursor,
} as ListToolsResult;
},
);
}
// Resources pagination
if (capabilities.resources && maxPageSize.resources !== undefined) {
mcpServer.server.setRequestHandler(
ListResourcesRequestSchema,
async (request, extra) => {
const cursor = request.params?.cursor;
const pageSize = maxPageSize.resources!;
// Collect all resources (static + from templates)
const allResources: Resource[] = [];
// Add static resources from registered resources
for (const [uri, registered] of state.registeredResources.entries()) {
if (registered.enabled) {
allResources.push({
uri,
name: registered.name,
title: registered.title,
description: registered.metadata?.description,
mimeType: registered.metadata?.mimeType,
icons: registered.metadata?.icons,
} as Resource);
}
}
// Add resources from templates (if list callback exists)
for (const template of state.registeredResourceTemplates.values()) {
if (template.enabled && template.resourceTemplate.listCallback) {
try {
const result =
await template.resourceTemplate.listCallback(extra);
for (const resource of result.resources) {
allResources.push({
...resource,
// Merge template metadata if resource doesn't have it
name: resource.name,
description:
resource.description || template.metadata?.description,
mimeType: resource.mimeType || template.metadata?.mimeType,
icons: resource.icons || template.metadata?.icons,
} as Resource);
}
} catch {
// Ignore errors from list callbacks
}
}
}
const startIndex = cursor ? parseInt(cursor, 10) : 0;
const endIndex = startIndex + pageSize;
const page = allResources.slice(startIndex, endIndex);
const nextCursor =
endIndex < allResources.length ? endIndex.toString() : undefined;
return {
resources: page,
nextCursor,
} as ListResourcesResult;
},
);
}
// Resource templates pagination
if (capabilities.resources && maxPageSize.resourceTemplates !== undefined) {
mcpServer.server.setRequestHandler(
ListResourceTemplatesRequestSchema,
async (request) => {
const cursor = request.params?.cursor;
const pageSize = maxPageSize.resourceTemplates!;
// Convert registered resource templates to ResourceTemplate format
const allTemplates: Array<{
uriTemplate: string;
name: string;
description?: string;
mimeType?: string;
icons?: Array<{
src: string;
mimeType?: string;
sizes?: string[];
theme?: "light" | "dark";
}>;
title?: string;
}> = [];
for (const [
uriTemplate,
registered,
] of state.registeredResourceTemplates.entries()) {
if (registered.enabled) {
// Find the name from config by matching uriTemplate
const templateDef = config.resourceTemplates?.find(
(t) => t.uriTemplate === uriTemplate,
);
allTemplates.push({
uriTemplate: registered.resourceTemplate.uriTemplate.toString(),
name: templateDef?.name || uriTemplate, // Fallback to uriTemplate if name not found
title: registered.title,
description:
registered.metadata?.description || templateDef?.description,
mimeType: registered.metadata?.mimeType,
icons: registered.metadata?.icons,
});
}
}
const startIndex = cursor ? parseInt(cursor, 10) : 0;
const endIndex = startIndex + pageSize;
const page = allTemplates.slice(startIndex, endIndex);
const nextCursor =
endIndex < allTemplates.length ? endIndex.toString() : undefined;
return {
resourceTemplates: page as ResourceTemplate[],
nextCursor,
} as ListResourceTemplatesResult;
},
);
}
// Prompts pagination
if (capabilities.prompts && maxPageSize.prompts !== undefined) {
mcpServer.server.setRequestHandler(
ListPromptsRequestSchema,
async (request) => {
const cursor = request.params?.cursor;
const pageSize = maxPageSize.prompts!;
// Convert registered prompts to Prompt format using the same logic as the SDK
const allPrompts: Prompt[] = [];
for (const [name, prompt] of state.registeredPrompts.entries()) {
if (prompt.enabled) {
// Use the same conversion logic the SDK uses (from mcp.js line 408-419)
const shape = prompt.argsSchema
? getObjectShape(prompt.argsSchema)
: undefined;
const arguments_ = shape
? Object.entries(shape).map(([argName, field]) => {
const description = getSchemaDescription(field);
const isOptional = isSchemaOptional(field);
return {
name: argName,
description,
required: !isOptional,
} as PromptArgument;
})
: undefined;
allPrompts.push({
name,
title: prompt.title,
description: prompt.description,
arguments: arguments_,
} as Prompt);
}
}
const startIndex = cursor ? parseInt(cursor, 10) : 0;
const endIndex = startIndex + pageSize;
const page = allPrompts.slice(startIndex, endIndex);
const nextCursor =
endIndex < allPrompts.length ? endIndex.toString() : undefined;
return {
prompts: page,
nextCursor,
} as ListPromptsResult;
},
);
}
return mcpServer;
}