@appport/mcp

中文文档
1.3.7发布于 2026/11/06Apache-2.0TypeScriptES Module近 30 天下载 2007

Expose AppPort capabilities as MCP tools.

原始使用文档
文档目录10 节
  1. Architecture
  2. Server setup
  3. Authentication
  4. Capability exposure
  5. Metadata
  6. Errors
  7. Security
  8. State
  9. Compatibility
  10. @appport/mcp/bridge (1.3.0)

@appport/mcp

The MCP adapter for AppPort: exposes an AppPort application's capabilities to any MCP client (agents, IDEs, other hosts) over the official @modelcontextprotocol/sdk.

Architecture

            AppPort  (authority: capabilities, authorization, idempotency, timeouts)
               │
     ┌─────────┴──────────┐
  native clients      @appport/mcp  ──►  MCP clients

AppPort is the authority; MCP is a projection. This package defines no capabilities, permissions, tenants, operations, persistence or business logic, and retains no state between calls. Each MCP tools/call is translated into an AppRequest and dispatched through AppPortApplication.handleRequest — the same pipeline every other transport uses — so validation, authorization, idempotency, timeouts, telemetry and handler execution are unchanged. JSON-RPC, protocol negotiation, framing and transports are the MCP SDK's; none is reimplemented here.

MCP tools/call → @appport/mcp → AppRequest → application.handleRequest
                                   → validate → authorize → capability handler

Server setup

createMcpHandler returns a standard (Request) => Promise<Response> function serving MCP Streamable HTTP. It is stateless — each HTTP request gets its own short-lived SDK server and transport and is answered as one JSON response — so it runs on serverless platforms and any fetch-compatible runtime without sticky sessions or a long-lived process. GET and DELETE (server-initiated streams, session teardown) are declined with 405, which MCP permits.

import { createMcpHandler } from "@appport/mcp";
import { errors } from "@appport/protocol";

const handler = createMcpHandler(application, {
  // Runs per MCP request. Verify credentials, then say who the caller is.
  async context({ headers, authInfo }) {
    const token = headers["authorization"]?.toString().replace(/^Bearer /, "");
    if (!token) return undefined; // anonymous: AppPort allows only public capabilities
    const session = await myApp.verify(token); // your AppPort `Session`
    if (!session) throw errors.unauthorized("Invalid credentials");
    return { session };
  }
});

// e.g. a Next.js / Vercel route handler
export const POST = (request: Request) => handler(request);
export const GET = POST;
export const DELETE = POST;

For stdio or in-memory use, createMcpServer(application, options) returns the SDK Server to connect to any SDK transport.

Options (McpServerOptions): context, serverInfo (defaults to the application's name and version), include, prefix, includeBuiltins.

The host owns deployment, routing, TLS, origin/DNS-rebinding checks, rate limiting and credential verification. The host may also verify the credential before calling the handler and pass it as handler(request, { authInfo }); it then reaches context as authInfo.

Authentication

The adapter never authenticates callers and never trusts them implicitly. The host supplies the authenticated session and/or principal — the same values a native transport passes in AppPort's DispatchOptions — through context.

  • No context option, or context returns nothing → the call is dispatched anonymously. AppPort's authorizer refuses any capability that requires permissions (UNAUTHORIZED); only public capabilities run.
  • context throws an AppPortError (UNAUTHORIZED/FORBIDDEN) → reported as a tool error. Any other exception is reported as a JSON-RPC internal error with no detail.
  • Identity cannot be supplied by the MCP caller in tool arguments or _meta.

Idempotency keys are scoped by AppPort to the session, so provide a stable, per-caller session.id.

Capability exposure

Tools are derived from the application manifest on every request; there is no second list.

AppPortMCP tool
request capability call.createtool call_create (. → _, optional prefix)
capability inputSchematool inputSchema (must be an object schema)
capability outputSchematool outputSchema (when an object schema)
capability name/version/permissionstool _meta["dev.appport/capability"]

The tool name is a label, not an identity: calls resolve through the manifest. Two capabilities projecting to one tool name is an error, not an overwrite. Stream capabilities, non-object inputs and the reserved appport.* namespace (unless includeBuiltins) are not exposed. Tool discovery lists what the application publishes; it does not filter by caller — authorization is enforced when a tool is called.

Metadata

AppPort request metadata is carried in the MCP tools/call _meta object and placed, unchanged, on the AppRequest (MCP_META exports the keys):

_meta keyAppPort fieldNotes
dev.appport/idempotencyKeyidempotencyKeyreplay is AppPort's (§13.1); this package keeps no cache
dev.appport/timeoutMstimeoutMsbecomes the capability's deadline; AppPort clamps it (§13.3); no MCP default is added
dev.appport/traceIdtraceIdreaches context.traceId; AppPort generates one if absent; echoed on the result _meta

Malformed values (empty strings, non-positive timeouts) are rejected as INVALID_REQUEST. MCP request cancellation and HTTP client disconnects abort the capability's context.signal.

Errors

Protocol-level problems use JSON-RPC errors from the SDK: malformed JSON or messages (-32700/-32600), unknown method (-32601), unknown tool (-32602), and adapter failures (-32603, no detail). Every AppPort outcome is a tool result: success has content (JSON text) and structuredContent; failure has isError: true, text CODE: message, and _meta["dev.appport/error"] (MCP_ERROR_META) with { code, category, retryable, details? }. details is what AppPort already sanitized (§16).

AppPort codecategory
INVALID_REQUEST, INVALID_INPUT, INVALID_PROTOCOL, UNSUPPORTED_VERSION, UNSUPPORTED_CAPABILITY_VERSION, PAYLOAD_TOO_LARGE, NO_COMPATIBLE_MATERIALIZATIONinvalid_request
UNAUTHORIZEDunauthenticated
FORBIDDENforbidden
NOT_FOUND, UNKNOWN_CAPABILITYnot_found
CONFLICTconflict
TIMEOUTtimeout
RATE_LIMITEDrate_limited
CANCELLEDcancelled
STREAM_ERROR, namespaced extension codes (acme.UPSTREAM_DOWN)capability_failure
INTERNAL_ERROR, anything elseinternal

Security

Exposing a capability through MCP does not bypass AppPort authorization: every call passes AppPort validation and the application's authorizer, and handlers receive the host-supplied session/principal. Tenant and resource isolation remain the application's responsibility, enforced in capability handlers through that context (for example context.session.attributes); a caller cannot reach another tenant's data through MCP any more than through any other transport.

State

None. Nothing is cached, stored or shared between calls; a fresh SDK server is built per HTTP request. Durable and ephemeral state (idempotency records, sessions, operations) belong to the AppPort application.

Compatibility

createMcpBridge, toMcpTools, toToolName, fromToolName and describeError remain. createMcpBridge(...).callTool(name, args, metadata?) now forwards the AppPort idempotency key, timeout and trace ID (previously dropped), and its results now carry structuredContent and _meta. Prefer createMcpHandler / createMcpServer, which speak MCP; fromToolName is lossy for capability names containing _ and is not used internally.

@appport/mcp/bridge (1.3.0)

@appport/mcp/bridge exports the projection only: createMcpBridge, toMcpTools, toToolName, fromToolName, MCP_META, MCP_ERROR_META, mcpErrorCategory and their types. It never imports @modelcontextprotocol/sdk, at runtime or in its declarations. The root @appport/mcp exports the same names plus the MCP server (createMcpServer, createMcpHandler), which does. The MCP SDK's own declarations reference the DOM-only HeadersInit, so a Node-only TypeScript project (no DOM lib, skipLibCheck: false) can import @appport/mcp/bridge but must either add the DOM lib or enable skipLibCheck to import the @appport/mcp root. @appport/sdk takes its MCP bridge exports from this subpath.

版本与发布记录

@appport/mcp 共发布 13 个版本,首次发布于 2026/09/21;过去 12 个月发布了 13 个版本。

版本发布日期距今
1.3.7latest2026/11/062 天前
1.3.62026/11/052 天前
1.3.52026/11/034 天前
1.3.42026/10/2810 天前
1.3.32026/10/2217 天前
1.3.22026/10/1919 天前

关于 @appport/mcp 的常见问题

@appport/mcp 是什么?

Expose AppPort capabilities as MCP tools.

如何安装 @appport/mcp?

在项目目录运行 npm install @appport/mcp,或使用 pnpm add / yarn add / bun add。

@appport/mcp 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@appport/mcp 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@appport/mcp 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@appport/mcp 可以商用吗?

@appport/mcp 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@appport/mcp 的下载量有多少?

根据 npm 官方下载统计,@appport/mcp 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,007 次,日均约 67 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appport/mcp 还在维护吗?

最新版本 1.3.7 发布于 2026/11/06(2 天前);至今共发布 13 个版本,其中过去 12 个月发布了 13 个;首次发布于 2026/09/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。