@contextgraphprotocol/typescript-sdk

中文文档
0.1.0发布于 2026/07/24MIT OR Apache-2.0TypeScriptES Module零依赖近 30 天下载 6899

Zero-dependency TypeScript SDK for building conformant Context Graph Protocol providers.

原始使用文档
文档目录5 节
  1. Install
  2. Write a provider
  3. What it gives you
  4. Prove it conformant
  5. License

@contextgraph/sdk — TypeScript

A zero-dependency TypeScript SDK for building conformant Context Graph Protocol providers. Implement one interface, hand it to the runtime, and you have a provider that speaks the line-oriented JSON wire over stdio and passes the same conformance suite that judges the Rust reference provider.

This is the second independent implementation (after the Rust reference), and it passes the full conformance suite — the concrete evidence behind the protocol's "enforced by contract, not convention" claim. See sdk/README.md for the multi-language picture.

Install

npm install @contextgraph/sdk

Write a provider

import { runStdioProvider, budgetTokens, type Provider } from "@contextgraph/sdk";

const provider: Provider = {
  info: () => ({
    name: "my-docs-provider",
    version: "0.1.0",
    // Nothing leaves the machine ⇒ declare the honest local-only egress scope.
    data_flow: { reads: true, writes: false, egress: false, egress_scopes: ["local-only"] },
  }),
  capabilities: () => ({ query: { kinds: ["doc"] }, correlation: true, verify: true }),
  query: () => ({
    frames: [
      {
        id: "doc:1",
        kind: "doc",
        title: "Getting started",
        content: "Install the binding, then implement the required methods.",
        content_digest: `sha256:${"11".repeat(32)}`,
        score: 0.9,
        // token_cost MUST equal ceil(utf8_len(content)/4) — the SDK computes it.
        token_cost: budgetTokens("Install the binding, then implement the required methods."),
        valid_from: "2026-01-01T00:00:00Z",
        provenance: [{ type: "file", uri: "file:///docs/start.md", range: "L1-10", digest: `sha256:${"11".repeat(32)}` }],
        citation_label: "start.md L1-10",
        relations: [],
      },
    ],
    truncated: false,
  }),
};

runStdioProvider(provider);

The runtime handles the whole lifecycle a host drives — handshake, query (echoing the correlation id), verify, shutdown — and stays alive with a typed error on a malformed line rather than crashing.

What it gives you

  • Wire types (ContextFrame, ContextQuery, Capabilities, Envelope, …) mirrored from the JSON Schema — the language-neutral source of truth.
  • runStdioProvider(provider) — the stdio lifecycle loop.
  • budgetTokens(content) — the canonical B3 cost, ceil(utf8_len/4).
  • A runnable example provider (examples/example-docs.ts) that passes all seven conformance checks.

Prove it conformant

Build, then run the reference suite against your provider:

npm run build
# from the repository root, with the Rust bins built (cargo build --workspace --bins):
./.github/scripts/conformance-external.sh -- node sdk/typescript/dist/examples/example-docs.js

A green run is the machine-checkable claim that your provider honors the protocol — the same bar every implementation is held to.

License

MIT OR Apache-2.0, matching the Context Graph Protocol crates.

版本与发布记录

@contextgraphprotocol/typescript-sdk 共发布 1 个版本,首次发布于 2026/07/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.0latest2026/07/243 个月前

关于 @contextgraphprotocol/typescript-sdk 的常见问题

@contextgraphprotocol/typescript-sdk 是什么?

Zero-dependency TypeScript SDK for building conformant Context Graph Protocol providers.

如何安装 @contextgraphprotocol/typescript-sdk?

在项目目录运行 npm install @contextgraphprotocol/typescript-sdk,或使用 pnpm add / yarn add / bun add。

@contextgraphprotocol/typescript-sdk 支持 TypeScript 吗?

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

@contextgraphprotocol/typescript-sdk 支持 ESM 还是 CommonJS?

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

@contextgraphprotocol/typescript-sdk 需要什么版本的 Node.js?

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

@contextgraphprotocol/typescript-sdk 可以商用吗?

@contextgraphprotocol/typescript-sdk 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contextgraphprotocol/typescript-sdk 的下载量有多少?

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

@contextgraphprotocol/typescript-sdk 还在维护吗?

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