TypeScript SDK
KYA-OS TypeScript SDK — @kya-os/mcp package documentation
@kya-os/mcp
The TypeScript reference implementation for KYA-OS. Identity, delegation, and cryptographic proofs for the Model Context Protocol.
Looking for the quick version?
If you just want to get running, start with the Quick Start. This page covers the full API surface.
Installation
npm install @kya-os/mcp
Requires Node.js 20+. Peer dependency on @modelcontextprotocol/sdk (optional, only needed for withKyaOs).
Core API
withKyaOs — One-Line Integration
The fastest path. Wraps an existing MCP server with identity, sessions, and proofs.
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { withKyaOs, NodeCryptoProvider } from "@kya-os/mcp";
const server = new McpServer({ name: "my-server", version: "1.0.0" });
const kyaos = await withKyaOs(server, { crypto: new NodeCryptoProvider() });
// Your server now has:
// - A DID identity (kyaos.identity.did)
// - Handshake endpoint (_kyaos tool)
// - Proofs on every tool response
// - Session management with replay prevention
Default behavior is compatibility-first. withKyaOs auto-registers _kyaos as a tool so it appears in MCP Inspector and tool lists.
Options
await withKyaOs(server, {
crypto: new NodeCryptoProvider(),
handshakeExposure: "none", // 'tool' (default) | 'none'
autoSession: false, // Skip automatic session creation
});
If your runtime has its own connection handshake, disable tool exposure and call the middleware directly:
const kyaos = await withKyaOs(server, {
crypto: new NodeCryptoProvider(),
handshakeExposure: "none",
autoSession: false,
});
// In your runtime's connection hook:
await kyaos.handleKyaOs({
action: "handshake",
nonce: "client-generated-nonce",
audience: kyaos.identity.did,
timestamp: Math.floor(Date.now() / 1000),
agentDid: "did:key:...optional...",
});
createKyaOsMiddleware — Low-Level Control
For when you need full control over identity lifecycle, session parameters, and tool wrapping.
import {
createKyaOsMiddleware,
generateIdentity,
NodeCryptoProvider,
} from "@kya-os/mcp";
const crypto = new NodeCryptoProvider();
const identity = await generateIdentity(crypto);
const kyaos = createKyaOsMiddleware(
{ identity, session: { sessionTtlMinutes: 60 } },
crypto
);
wrapWithProof — Automatic Proof Attachment
Wraps a tool handler so every response includes a detached JWS signature.
const search = kyaos.wrapWithProof("search", async (args) => ({
content: [{ type: "text", text: `Results for: ${args["query"]}` }],
}));
wrapWithDelegation — Human Consent Required
Wraps a tool handler so it requires a valid delegation VC before executing.
const placeOrder = kyaos.wrapWithDelegation(
"place_order",
{
scopeId: "orders:write",
consentUrl: "https://example.com/consent",
},
kyaos.wrapWithProof("place_order", async (args) => ({
content: [{ type: "text", text: `Order placed: ${args["item"]}` }],
}))
);
If the agent doesn't have a valid delegation, the tool returns needs_authorization with the consent URL. The human approves, the agent gets a VC, and retries.
generateIdentity — Create a DID
Generates an Ed25519 key pair and derives a did:key identifier.
import { generateIdentity, NodeCryptoProvider } from "@kya-os/mcp";
const crypto = new NodeCryptoProvider();
const identity = await generateIdentity(crypto);
console.log(identity.did);
// did:key:z6Mk...
Modules
All available as subpath exports:
| Import | What it does |
|---|---|
@kya-os/mcp | Main entry. withKyaOs, createKyaOsMiddleware, generateIdentity, providers. |
@kya-os/mcp/delegation | Issue and verify W3C Verifiable Credentials. DID resolution. StatusList2021 revocation. Cascading revocation. |
@kya-os/mcp/proof | Generate and verify detached JWS proofs with canonical hashing (JCS + SHA-256). |
@kya-os/mcp/session | Nonce-based handshake. Replay prevention. Session TTL management. |
@kya-os/mcp/providers | Abstract CryptoProvider, IdentityProvider, StorageProvider. |
@kya-os/mcp/types | Pure TypeScript interfaces. Zero runtime dependencies. |
Extension Points
Every cryptographic operation, storage backend, and DID resolution method is abstracted behind interfaces you implement.
Custom Crypto Provider
import { CryptoProvider } from "@kya-os/mcp";
class KMSCryptoProvider extends CryptoProvider {
async sign(data: Uint8Array, keyArn: string) {
return kmsClient.sign({ KeyId: keyArn, Message: data });
}
}
await withKyaOs(server, { crypto: new KMSCryptoProvider() });
Custom Nonce Cache
import { NonceCacheProvider } from "@kya-os/mcp";
class RedisNonceCacheProvider extends NonceCacheProvider {
async hasNonce(nonce: string) {
return redis.exists(`nonce:${nonce}`);
}
async addNonce(nonce: string, ttl: number) {
redis.setex(`nonce:${nonce}`, ttl, "1");
}
}
DID Resolution
Built-in support for did:key (self-resolving) and did:web (HTTP resolution). Add any custom method via DIDResolver.
Conformance Levels
Three levels, progressively deeper trust guarantees:
| Level | What's required |
|---|---|
| Level 1 — Core Crypto | Ed25519 signatures, DID:key resolution, JCS canonicalization |
| Level 2 — Full Session | Level 1 + nonce-based handshake, session management, replay prevention |
| Level 3 — Full Delegation | Level 2 + W3C VC issuance/verification, scope attenuation, StatusList2021, cascading revocation |
Full details in CONFORMANCE.md.
Examples
| Example | What it shows |
|---|---|
| consent-basic | Human-in-the-loop consent: needs_authorization → consent page → delegation VC → execution |
| consent-full | Production consent UI via @kya-os/consent |
| node-server | Low-level Server API with handshake, proof, and restricted tools |
| brave-search | Real MCP server (Brave Search) wrapped with KYA-OS |
| outbound-delegation | Forwarding delegation context to downstream services |
| verify-proof | Standalone proof verification |
| context7-with-kyaos | Adding KYA-OS to an existing server with withKyaOs |
Open standard, open governance
@kya-os/mcp is the DIF TAAWG reference implementation. Governed by lazy consensus for non-breaking changes, explicit vote for breaking changes. DCO sign-off required on all contributions. See GOVERNANCE.md.