import { type ClientCapabilities, type Implementation, type RequestStateAccessor, type ServerContext } from "@modelcontextprotocol/server"; import type { Context, Env, HonoRequest as HonoRequestType } from "hono"; /** * OpenAI-specific end-user hints declared in request metadata. * * All fields are optional, client-reported, and unverified. Use * {@link OAuthAuth.user} for an authenticated identity. */ export interface UserContext { /** Requested BCP 47 locale from `openai/locale` or legacy `webplus/i18n`. */ locale?: string; /** Best-effort browser or host identifier from `openai/userAgent`. */ userAgent?: string; /** Coarse end-user location from `openai/userLocation`. */ location?: { /** City name, when supplied. */ city?: string; /** Region or state name, when supplied. */ region?: string; /** Country identifier, when supplied. */ country?: string; /** IANA timezone identifier, when supplied. */ timezone?: string; /** Approximate latitude; clients may encode coordinates as strings or numbers. */ latitude?: string | number; /** Approximate longitude; clients may encode coordinates as strings or numbers. */ longitude?: string | number; }; /** Client-reported subject hint from `openai/subject`. */ subject?: string; /** Client-reported conversation hint from `openai/session`. */ conversationId?: string; /** Client-reported organization hint from `openai/organization`. */ organizationId?: string; } /** * Per-request client metadata and capability queries. * * Reads only the current request's metadata — never session state. */ export interface RequestClientContext { /** * Checks whether this request declares a top-level client capability. * * @param capability - Capability name such as `sampling`, `elicitation`, * `roots`, or `extensions`. * @returns `true` when the current request advertises the capability. * * @example * ```ts * if (ctx.client.can("elicitation")) { * // Shape the result for an elicitation-capable client. * } * ``` */ can(capability: string): boolean; /** * Returns the capabilities declared by the client for this request. * * The returned object is a shallow copy. Requests without the modern * metadata envelope return an empty object. * * @example * ```ts * const capabilities = ctx.client.capabilities(); * const supportsFormElicitation = capabilities.elicitation?.form !== undefined; * ``` */ capabilities(): ClientCapabilities; /** * Returns one extension settings object declared for this request. * * @param id - Namespaced extension identifier, such as * `io.modelcontextprotocol/ui`. * @returns A shallow copy of the extension settings, or `undefined` when the * current request does not advertise the extension. * * @example * ```ts * const ui = ctx.client.extension("io.modelcontextprotocol/ui"); * ``` */ extension(id: string): NonNullable[string] | undefined; /** * Returns the client implementation metadata declared for this request. * * Valid modern requests include `name` and `version`. The partial return * type preserves v1 ergonomics for legacy requests without an envelope. * The returned object is a shallow copy. * * @example * ```ts * const { name, version } = ctx.client.info(); * ``` */ info(): Partial; /** * Returns normalized OpenAI-specific end-user hints for this request. * * The data comes from ordinary request `_meta`, not the MCP client-info * envelope. It is client-reported and must not be used for authentication * or authorization. Each call returns a fresh object, including a fresh * location object. Requests without recognized metadata return `undefined`. * * @example * ```ts * const caller = ctx.client.user(); * const locale = caller?.locale ?? "en"; * ``` */ user(): UserContext | undefined; /** * Whether this request's client advertises MCP Apps / UI support. * * True when the client declares the `io.modelcontextprotocol/ui` extension * with `text/html;profile=mcp-app` in `mimeTypes`. Legacy (non-envelope) * requests always return `false`. * * @example * ```ts * if (ctx.client.supportsViews()) { * // Return a view-optimized result. * } * ``` */ supportsViews(): boolean; } /** * Per-request context passed to tool/resource/prompt callbacks. */ export type OAuthAuth = { /** Authenticated application user mapped by the OAuth provider. */ user: TUser; /** Verified access-token claims or introspection data. */ payload: Record; /** Raw bearer token for authenticated downstream requests. */ accessToken: string; /** OAuth scopes granted to the access token. */ scopes: string[]; /** Provider-normalized permissions granted to the user. */ permissions: string[]; /** * The OAuth client identifier from the token's `client_id` or `azp` claim; * undefined when the identity provider's access tokens carry no client claim * (e.g. WorkOS AuthKit, Supabase). */ clientId?: string; /** Access-token expiration as Unix time in seconds. */ expiresAt: number; /** Resource audience the access token authorizes, when supplied. */ resource?: URL; /** * The upstream identity provider's token for the signed-in user, when the * server is an OAuth proxy (see `oauthProxy` in `mcp-use/oauth`). Use it to * call the upstream API, for example GitHub, as the user. `accessToken` * stays the proxy's own token. Do not return this value to the client, and * do not put it in tool results, views, or `_meta`. */ upstream?: OAuthUpstreamToken; }; /** Upstream identity-provider token that an OAuth proxy holds for a user. */ export interface OAuthUpstreamToken { /** Bearer token for the upstream API. */ accessToken: string; /** Scopes the upstream provider granted. */ scopes: string[]; /** Upstream token expiry as Unix time in seconds, when the provider gives one. */ expiresAt?: number; } type RequestContextBase = Omit, "req"> & { /** Aborted when the client cancels the request or the connection drops. */ signal: AbortSignal; /** Hono request for the originating HTTP exchange. Its raw Web Request is available as `request.raw`. */ request?: HonoRequestType; /** * Deprecated v1-compatible alias for {@link RequestContext.request}. * * @deprecated Use `request` instead. */ req?: HonoRequestType; /** Per-request client capability queries. */ client: RequestClientContext; /** * Bare responses supplied when the client retries an `input_required` * round. Values are client input; validate them before use. */ inputResponses?: ServerContext["mcpReq"]["inputResponses"]; /** * Read opaque state echoed by the client for this `input_required` round. * The generic is only a type assertion. Configure `requestState.verify` * when state affects authorization or business logic. */ requestState: RequestStateAccessor; /** * Sends a one-way notification related to the current MCP request. * * The notification is delivered on the originating request's response * stream and must be sent before the callback returns. Use the * `server.notifyToolsChanged()` and related server helpers for * cross-request list or resource invalidations. * * @param method - Application-defined notification method. * @param params - Optional JSON-serializable notification parameters. * @returns A promise that resolves after the notification is sent. * * @example * ```ts * await ctx.sendNotification("com.example/import-status", { * status: "started", * }); * ``` */ sendNotification(method: string, params?: Record): Promise; /** * Report request-scoped progress when the caller supplied a progress token. * * @returns `true` when a notification was sent, `false` when the caller did * not request progress. */ reportProgress(progress: number, total?: number, message?: string): Promise; /** Send a log message notification to the client during tool execution. */ sendLog(level: "debug" | "info" | "notice" | "warning" | "error" | "critical" | "alert" | "emergency", data: unknown, logger?: string): Promise; }; /** * How verified OAuth identity reaches a callback's `ctx.auth`. * * - `false`: no OAuth provider is configured; `auth` is never present. * - `true`: the item requires sign-in, so `auth` is always present. * - `"optional"`: the tool accepts `noauth`, so `auth` is present only when * the request carried a verified token. */ export type OAuthMode = boolean | "optional"; /** `securitySchemes` that require sign-in: no `noauth` entry. */ type SignInSchemes = readonly { readonly type: "oauth2"; }[]; /** * The {@link OAuthMode} of one tool's callback, read from its literal * `securitySchemes`. Anything that might accept `noauth`, including a widened * array, yields an optional `ctx.auth`. * * @internal */ export type ToolOAuthMode = [TUser] extends [never] ? false : [TSchemes] extends [SignInSchemes | undefined] ? true : "optional"; /** * The `securitySchemes` of a tool definition type, or `undefined` when it * has none. * * @internal */ export type DefinitionSecuritySchemes = "securitySchemes" extends keyof T ? T["securitySchemes"] : undefined; /** * Per-request callback context, authenticated when OAuth is configured. * * The `HasOAuth` parameter is an {@link OAuthMode}: `true` guarantees * `auth`, `"optional"` makes it nullable for tools that accept `noauth`, and * `false` removes it. */ export type RequestContext = HasOAuth extends true ? RequestContextBase & { auth: OAuthAuth; } : HasOAuth extends "optional" ? RequestContextBase & { auth?: OAuthAuth; } : RequestContextBase & { auth?: never; }; /** * The requesting client's advertised identity for this request. * * Read from the per-request `_meta` envelope, so it is the identity of the * client that sent *this* request. Empty on 2025-era (legacy) traffic, which * carries no per-request envelope. * * @param ctx - SDK per-request server context. * @returns The declared `clientInfo`, or an empty object when the request * carried none. * * @internal */ export declare function requestClientInfo(ctx: ServerContext): Partial; /** * Derive the callback-facing {@link RequestContext} from the SDK's * per-request `ServerContext`. * * @internal */ export declare function toRequestContext(ctx: ServerContext): RequestContext; /** @internal Projects mapped OAuth auth information into callback context. */ export declare function toAuthenticatedRequestContext(ctx: ServerContext): RequestContext; export {};