import type { CallToolResult, Icon, InputRequiredResult, MetaObject, StandardSchemaWithJSON, ToolAnnotations } from "@modelcontextprotocol/server"; import type { Env } from "hono"; import type { McpUiResourceCsp } from "@modelcontextprotocol/ext-apps"; import type { OAuthMode, RequestContext } from "./context.js"; import type { UiPermissions } from "./views/types.js"; /** * One way a client may call a tool, from the `securitySchemes` convention * ChatGPT reads on `tools/list`. See {@link ToolDefinition.securitySchemes}. */ export type ToolSecurityScheme = { /** The tool runs without a token. */ readonly type: "noauth"; } | { /** The tool accepts an OAuth 2.1 bearer token. */ readonly type: "oauth2"; /** * Scopes beyond the provider's `requiredScopes`. Use `[]` for the * provider's `requiredScopes` alone. */ readonly scopes: readonly string[]; }; /** Manual launch locations advertised by ChatGPT on a view-bound tool. */ export type ToolViewEntrypoint = { /** Opens the app from the global launcher. */ readonly type: "global"; } | { /** Opens the app within the current conversation. */ readonly type: "thread"; } | { /** Opens a matching file in the app. */ readonly type: "file"; /** HTML accept-style file extensions, each beginning with a dot. */ readonly extensions: readonly string[]; }; /** Arguments supplied by the host when opening a file entrypoint. */ export interface FileEntrypointInput { /** Host-provided file identity and resource reference. */ file: { /** File name including its extension, without a filesystem path. */ name: string; /** Opaque host resource URI. */ resourceUri: string; }; } /** * Binds a tool to a view directory for MCP Apps rendering. * * A view may have at most one bound tool. That tool owns the resource facts * (`description`, `csp`, `permissions`, `domain`, `prefersBorder`); a second * tool that names the same view is rejected. The view file exports the * component, while the framework emits these fields on the view's MCP * resource, where hosts read `_meta.ui`. */ export interface ToolViewConfig { /** View directory / registry name, e.g. `"product-search-result"`. */ name: string; /** * Manual launch locations → tool `_meta["openai/ui"].entrypoints`. * Global/thread launchers must accept `{}`; file launchers require * {@link FileEntrypointInput}. Entrypoints open fullscreen in supporting * hosts. Typed entrypoints take precedence over raw metadata declarations. * Registration checks the input JSON Schema against `{}` and representative * file payloads for the declared extensions. Application refinements must * also accept host launch arguments; they only run on actual tool calls. */ entrypoints?: readonly ToolViewEntrypoint[]; /** * Human-readable description of the view resource → the resource's * `description` on `resources/list`, and `_meta["openai/widgetDescription"]` * on `resources/list` and `resources/read`. */ description?: string; /** * CSP domains the host must allow → resource `_meta.ui.csp`. The framework * appends the server origin to `connectDomains` and the configured assets * origin (or server origin) to `resourceDomains` at emission time. * `frameDomains` and `baseUriDomains` merge authored and category-specific * environment lists; each field is omitted when its merged list is empty. * Other author-set fields pass through. */ csp?: McpUiResourceCsp; /** Sandbox permissions the view needs → resource `_meta.ui.permissions`. */ permissions?: UiPermissions; /** * Dedicated origin hint for hosts that render views on a separate domain → * resource `_meta.ui.domain`. */ domain?: string; /** * Ask the host to draw a border around the view → resource * `_meta.ui.prefersBorder`. */ prefersBorder?: boolean; } /** Declares a tool's identity, LLM-facing description, and schemas. First argument to {@link MCPServer.tool}. */ export interface ToolDefinition { /** Unique tool identifier, e.g. `"fetch-weather"`. */ name: string; /** Human-readable title shown in UIs (falls back to `name`). */ title?: string; /** LLM-facing description of what the tool does. */ description?: string; /** * Tool icons advertised on `tools/list`, in author order. Absolute HTTP(S) * and data URLs pass through; paths relative to `public/` become absolute * asset URLs for HTTP requests. Entrypoint hosts prefer these icons over * server branding. Use monochrome SVGs with `currentColor` for ChatGPT. */ icons?: Icon[]; /** * Object schema for input validation — any Standard Schema library with * JSON Schema conversion ({@link StandardSchemaWithJSON}): zod v4, ArkType, * Valibot, …. Field descriptions become LLM hints. Input is validated by * the SDK before the callback runs. Emitted on the wire as `inputSchema`. */ inputSchema?: StandardSchemaWithJSON; /** * Alias for {@link ToolDefinition.inputSchema}. Prefer `inputSchema` in new * code — it matches the MCP wire field name. */ schema?: StandardSchemaWithJSON; /** * Schema for structured output ({@link StandardSchemaWithJSON}). Any JSON * root is allowed — object, array, or primitive (2026-07-28 protocol). * When set, the callback must return a result carrying matching * `structuredContent` or an explicit `isError` result — enforced at * compile time and validated by the SDK at runtime. */ outputSchema?: StandardSchemaWithJSON; /** Behavioral hints for clients (readOnlyHint, destructiveHint, …). */ annotations?: ToolAnnotations; /** * Extension metadata advertised on this tool's `tools/list` descriptor. * * Use vendor-namespaced keys for custom data. This definition metadata is * distinct from a tool callback result's `_meta`: it describes the tool, * while result `_meta` belongs to one invocation. For view-bound or * visibility-restricted tools, framework-owned MCP Apps keys are derived * from {@link ToolDefinition.view} and {@link ToolDefinition.visibility}; * those derived values take precedence over colliding entries here. */ _meta?: MetaObject; /** * Declares who may call or see the tool. Emitted as `_meta.ui.visibility` * on `tools/list`. Omitted = host default (callable by the model, visible * to the app). The server always lists every registered tool — hosts * filter by this declaration; the server never omits tools from * `tools/list`. `"app"` marks app-private helper tools callable from * views via `useCallTool` while the host hides them from the model. */ visibility?: "model" | "app"; /** * How the tool may be called on an OAuth server. Checked before the * callback runs, and advertised on `tools/list` for hosts such as ChatGPT. * * - `[{ type: "noauth" }]`: anyone may call it; `ctx.auth` is set when the * caller is signed in. * - `[{ type: "noauth" }, { type: "oauth2", scopes }]`: anyone may call it. * The scopes are only advertised, so the callback checks * `ctx.auth?.scopes` itself. * - `[{ type: "oauth2", scopes }]`: sign-in required with every listed * scope plus the provider's `requiredScopes`. * * Omitted means sign-in with the provider's `requiredScopes`. `noauth` * requires `mixedAuth: true` on the server. * * @example * ```ts * securitySchemes: [{ type: "noauth" }] * securitySchemes: [{ type: "oauth2", scopes: ["checkout"] }] * ``` */ securitySchemes?: readonly ToolSecurityScheme[]; /** * Bind this tool to a view for MCP Apps rendering. Requires * {@link ToolDefinition.outputSchema} — the view reads the result's * `structuredContent` typed by this schema. */ view?: ToolViewConfig; } /** * Handle returned by {@link MCPServer.tool} — carries the tool name at runtime * and phantom input/output types for inference (e.g. `Register` / `useCallTool`). */ export interface ToolRef, Output = never> { /** The tool's registered name. */ readonly name: Name; /** * Phantom carrier for the tool's inferred input/output types. Never set at * runtime. * * @internal */ readonly "~types"?: { input: Input; output: Output; }; } /** * Result a tool callback may return, keyed by the tool's inferred output * type. Prefer the SDK's raw {@link CallToolResult}; interactive calls may * return an {@link InputRequiredResult}. Deprecated response helpers * (`text`, `object`, …) return the same envelope and remain accepted. * * Without an `outputSchema` (`TOutput = never`) any `CallToolResult` is * accepted. With one, the result must carry `structuredContent` matching the * schema or set `isError: true` — mirroring the SDK's runtime rule, which * rejects results from schema'd tools that carry no structured content * unless `isError` is set. * * The SDK auto-appends a JSON text block when `structuredContent` is a * non-object value and no `type: "text"` block is present; for object-shaped * payloads include the text serialization yourself: * * ```ts * return { * content: [{ type: "text", text: JSON.stringify(data) }], * structuredContent: data, * }; * ``` */ export type ToolResult = InputRequiredResult | ([TOutput] extends [never] ? CallToolResult : (CallToolResult & { structuredContent: TOutput; }) | (CallToolResult & { isError: true; })); /** Infer the callback params type from a tool definition's input schema. */ export type InferToolInput = T extends { inputSchema: infer S extends StandardSchemaWithJSON; } ? StandardSchemaWithJSON.InferOutput extends Record ? StandardSchemaWithJSON.InferOutput : Record : T extends { schema: infer S extends StandardSchemaWithJSON; } ? StandardSchemaWithJSON.InferOutput extends Record ? StandardSchemaWithJSON.InferOutput : Record : Record; /** * Resolve a tool definition's input schema. `inputSchema` wins when both are * set. * * @internal */ export declare function resolveToolInputSchema(definition: Pick): StandardSchemaWithJSON | undefined; /** * Infer the structured output type from a definition's `outputSchema` — * any JSON root, not just objects (2026-07-28 protocol); `never` when the * definition declares none (any {@link CallToolResult} is then accepted — * see {@link ToolResult}). */ export type InferToolOutput = T extends { outputSchema: infer S extends StandardSchemaWithJSON; } ? StandardSchemaWithJSON.InferOutput : never; /** Infer the literal tool name from a definition's `name` field. */ export type InferToolName = T extends { name: infer N extends string; } ? N : string; /** * Tool execution callback. The return type is {@link ToolResult}: tools with * an `outputSchema` must return matching `structuredContent` or an `isError` * result; tools without one accept any {@link CallToolResult}. */ export type ToolCallback, TOutput = never, TUser = never, HasOAuth extends OAuthMode = false, TEnv extends Env = Env> = (params: TInput, ctx: RequestContext) => ToolResult | Promise>;