Skip to content

@codesoul-co/hypha-mcp / oauth

模块用法

用于使用该功能边界的公共契约与操作。Oauth 模块公开 1 类、2 函数、5 接口。

从包入口导入

ts
import {
  MCPOAuth21Client,
  mcpProtectedResourceMetadataUrlFromChallenge,
  redactMCPOAuthSecrets,
} from '@codesoul-co/hypha-mcp';

import type {
  MCPAuthorizationServerMetadata,
  MCPOAuth21ClientOptions,
  MCPOAuthAuthorizationRequest,
  MCPOAuthTokenSet,
  MCPProtectedResourceMetadata,
} from '@codesoul-co/hypha-mcp';

使用要点

  • 5 个类型/接口用于应用代码、Adapter 或测试中的静态契约;请使用 import type,运行时不应依赖它们。
  • 1 个类提供可实例化的运行时实现;构造参数与公开方法在各自条目中完整列出。
  • 2 个函数是该模块的直接操作入口;每个 overload 的必需/可选参数与返回类型均在下方列出。

公共导出

Symbol种类签名说明
MCPOAuth21Clientnew MCPOAuth21Client(options: MCPOAuth21ClientOptions): MCPOAuth21ClientMCPO Auth21 Client 类,共公开 7 个构造函数或成员;精确签名见本条目的声明与成员表。
mcpProtectedResourceMetadataUrlFromChallenge函数mcpProtectedResourceMetadataUrlFromChallenge(wwwAuthenticate: string | null | undefined): string | undefinedExtracts RFC 9728 metadata discovery from an MCP Bearer challenge.
redactMCPOAuthSecrets函数redactMCPOAuthSecrets<T>(value: T): TRedact MCPO Auth Secrets 函数,提供 1 个公开调用签名;参数与返回类型见下表。
MCPAuthorizationServerMetadata接口interface MCPAuthorizationServerMetadataMCP Authorization Server Metadata 接口,共包含 5 个公开字段或方法。
MCPOAuth21ClientOptions接口interface MCPOAuth21ClientOptionsMCPO Auth21 Client Options 接口,共包含 10 个公开字段或方法。
MCPOAuthAuthorizationRequest接口interface MCPOAuthAuthorizationRequestMCPO Auth Authorization Request 接口,共包含 3 个公开字段或方法。
MCPOAuthTokenSet接口interface MCPOAuthTokenSetMCPO Auth Token Set 接口,共包含 6 个公开字段或方法。
MCPProtectedResourceMetadata接口interface MCPProtectedResourceMetadataMCP Protected Resource Metadata 接口,共包含 3 个公开字段或方法。

MCPOAuth21Client

MCPO Auth21 Client 类,共公开 7 个构造函数或成员;精确签名见本条目的声明与成员表。

  • 种类: 类
  • 导入: import { MCPOAuth21Client } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export declare class MCPOAuth21Client {
    constructor(options: MCPOAuth21ClientOptions);
    discover(): Promise<{
            protectedResource: MCPProtectedResourceMetadata;
            authorizationServer: MCPAuthorizationServerMetadata;
        }>;
    createAuthorizationRequest(state?: string): Promise<MCPOAuthAuthorizationRequest>;
    exchangeAuthorizationCode(input: {
            code: string;
            codeVerifier: string;
            state: string;
            expectedState: string;
        }): Promise<MCPOAuthTokenSet>;
    refresh(): Promise<MCPOAuthTokenSet>;
    authorizationHeader(minimumValidityMs?: number): Promise<string>;
    clear(): void;
}

公开成员

成员种类签名说明
authorizationHeader方法authorizationHeader(minimumValidityMs?: number): Promise<string>公开方法;参数与返回类型以签名列为准。
clear方法clear(): void公开方法;参数与返回类型以签名列为准。
constructor构造函数(options: MCPOAuth21ClientOptions): MCPOAuth21Client创建该类的实例。
createAuthorizationRequest方法createAuthorizationRequest(state?: string): Promise<MCPOAuthAuthorizationRequest>公开方法;参数与返回类型以签名列为准。
discover方法discover(): Promise<{ protectedResource: MCPProtectedResourceMetadata; authorizationServer: MCPAuthorizationServerMetadata; }>公开方法;参数与返回类型以签名列为准。
exchangeAuthorizationCode方法exchangeAuthorizationCode(input: { code: string; codeVerifier: string; state: string; expectedState: string; }): Promise<MCPOAuthTokenSet>公开方法;参数与返回类型以签名列为准。
refresh方法refresh(): Promise<MCPOAuthTokenSet>公开方法;参数与返回类型以签名列为准。

mcpProtectedResourceMetadataUrlFromChallenge

Extracts RFC 9728 metadata discovery from an MCP Bearer challenge.

  • 种类: 函数
  • 导入: import { mcpProtectedResourceMetadataUrlFromChallenge } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export declare function mcpProtectedResourceMetadataUrlFromChallenge(wwwAuthenticate: string | null | undefined): string | undefined;

调用签名

text
mcpProtectedResourceMetadataUrlFromChallenge(wwwAuthenticate: string | null | undefined): string | undefined

Extracts RFC 9728 metadata discovery from an MCP Bearer challenge.

参数

参数类型必需说明
wwwAuthenticatestring必需参数;接受的值由类型列定义。

返回值

  • 类型: string
  • 说明: 返回值契约由上述类型定义。

redactMCPOAuthSecrets

Redact MCPO Auth Secrets 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { redactMCPOAuthSecrets } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export declare function redactMCPOAuthSecrets<T>(value: T): T;

调用签名

text
redactMCPOAuthSecrets<T>(value: T): T

参数

参数类型必需说明
valueT必需参数;接受的值由类型列定义。

返回值

  • 类型: T
  • 说明: 返回值契约由上述类型定义。

MCPAuthorizationServerMetadata

MCP Authorization Server Metadata 接口,共包含 5 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MCPAuthorizationServerMetadata } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export interface MCPAuthorizationServerMetadata {
    issuer: string;
    authorization_endpoint: string;
    token_endpoint: string;
    code_challenge_methods_supported: string[];
    grant_types_supported?: string[];
}

契约成员

成员种类签名说明
authorization_endpoint属性authorization_endpoint: string公开属性;类型、只读和可选状态以签名列为准。
code_challenge_methods_supported属性code_challenge_methods_supported: string[]公开属性;类型、只读和可选状态以签名列为准。
grant_types_supported属性grant_types_supported?: string[]公开属性;类型、只读和可选状态以签名列为准。
issuer属性issuer: string公开属性;类型、只读和可选状态以签名列为准。
token_endpoint属性token_endpoint: string公开属性;类型、只读和可选状态以签名列为准。

MCPOAuth21ClientOptions

MCPO Auth21 Client Options 接口,共包含 10 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MCPOAuth21ClientOptions } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export interface MCPOAuth21ClientOptions {
    resource: string;
    clientId: string;
    redirectUri: string;
    fetch?: typeof fetch;
    metadataUrl?: string;
    /** Selects one server advertised by RFC 9728 metadata; defaults to the first. */
    authorizationServer?: string;
    timeoutMs?: number;
    now?: () => number;
    randomBytes?: (size: number) => Uint8Array;
    /**
     * Intended only for an in-process acceptance server. Production callers
     * must leave this disabled so OAuth metadata and token traffic require TLS.
     */
    allowInsecureLoopbackForAcceptance?: boolean;
}

契约成员

成员种类签名说明
allowInsecureLoopbackForAcceptance属性allowInsecureLoopbackForAcceptance?: booleanIntended only for an in-process acceptance server. Production callers must leave this disabled so OAuth metadata and token traffic require TLS.
authorizationServer属性authorizationServer?: stringSelects one server advertised by RFC 9728 metadata; defaults to the first.
clientId属性clientId: string公开属性;类型、只读和可选状态以签名列为准。
fetch方法fetch?(input: RequestInfo | URL, init?: RequestInit): Promise<Response> | fetch?(input: string | URL | Request, init?: RequestInit): Promise<Response>公开方法;参数与返回类型以签名列为准。
metadataUrl属性metadataUrl?: string公开属性;类型、只读和可选状态以签名列为准。
now方法now?(): number公开方法;参数与返回类型以签名列为准。
randomBytes方法randomBytes?(size: number): Uint8Array公开方法;参数与返回类型以签名列为准。
redirectUri属性redirectUri: string公开属性;类型、只读和可选状态以签名列为准。
resource属性resource: string公开属性;类型、只读和可选状态以签名列为准。
timeoutMs属性timeoutMs?: number公开属性;类型、只读和可选状态以签名列为准。

MCPOAuthAuthorizationRequest

MCPO Auth Authorization Request 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MCPOAuthAuthorizationRequest } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export interface MCPOAuthAuthorizationRequest {
    url: string;
    state: string;
    codeVerifier: string;
}

契约成员

成员种类签名说明
codeVerifier属性codeVerifier: string公开属性;类型、只读和可选状态以签名列为准。
state属性state: string公开属性;类型、只读和可选状态以签名列为准。
url属性url: string公开属性;类型、只读和可选状态以签名列为准。

MCPOAuthTokenSet

MCPO Auth Token Set 接口,共包含 6 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MCPOAuthTokenSet } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export interface MCPOAuthTokenSet {
    accessToken: string;
    refreshToken?: string;
    tokenType: 'Bearer';
    expiresAt: number;
    scope?: string;
    resource: string;
}

契约成员

成员种类签名说明
accessToken属性accessToken: string公开属性;类型、只读和可选状态以签名列为准。
expiresAt属性expiresAt: number公开属性;类型、只读和可选状态以签名列为准。
refreshToken属性refreshToken?: string公开属性;类型、只读和可选状态以签名列为准。
resource属性resource: string公开属性;类型、只读和可选状态以签名列为准。
scope属性scope?: string公开属性;类型、只读和可选状态以签名列为准。
tokenType属性tokenType: "Bearer"公开属性;类型、只读和可选状态以签名列为准。

MCPProtectedResourceMetadata

MCP Protected Resource Metadata 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MCPProtectedResourceMetadata } from '@codesoul-co/hypha-mcp';
  • 源码模块: oauth

声明

text
export interface MCPProtectedResourceMetadata {
    resource: string;
    authorization_servers: string[];
    bearer_methods_supported?: string[];
}

契约成员

成员种类签名说明
authorization_servers属性authorization_servers: string[]公开属性;类型、只读和可选状态以签名列为准。
bearer_methods_supported属性bearer_methods_supported?: string[]公开属性;类型、只读和可选状态以签名列为准。
resource属性resource: string公开属性;类型、只读和可选状态以签名列为准。