@contextvm/sdk

中文文档
0.14.3发布于 2026/09/28LGPL-3.0-1TypeScriptES Module近 30 天下载 1.9万

ContextVM SDK for JavaScript/TypeScript

原始使用文档
文档目录9 节
  1. Overview
  2. Installation
  3. Package Structure
  4. Primary Entrypoint (Recommended)
  5. Subpath Imports (Tree-shaking)
  6. Usage
  7. Logging
  8. Development
  9. Running Tests

ContextVM SDK

A JavaScript/TypeScript SDK that implements the Context Vending Machine Protocol, bridging Nostr and Model Context Protocol (MCP) to enable decentralized access and exposure of computational services.

Overview

The ContextVM Protocol defines how Nostr and Model Context Machines can be used to expose MCP server capabilities, enabling standardized usage of these resources through a decentralized, cryptographically secure messaging system.

This SDK provides the necessary components to interact with the ContextVM Protocol:

  • Core Module: Contains fundamental definitions, constants, interfaces, and utilities (e.g., encryption, serialization).
  • Transports: Critical for communication, providing NostrClientTransport and NostrServerTransport implementations for enabling MCP over Nostr.
  • Proxy: A client-side MCP server that connects to other servers through Nostr, exposing server capabilities locally. Particularly useful for clients that don't natively support Nostr transport.
  • Gateway: Implements Nostr server transport, binding to another MCP server and exposing its capabilities through the Nostr network.
  • Relay: Functionality for managing Nostr relays, abstracting relay interactions.
  • Signer: Provides cryptographic signing capabilities required for Nostr events.

Both the Proxy and Gateway leverage Nostr transports, allowing existing MCP servers to maintain their conventional transports while gaining Nostr interoperability.

Installation

npm install @contextvm/sdk

Note: You can use your preferred package manager to install the SDK.

Package Structure

The SDK supports two import patterns:

Import from the root for convenience:

import { NostrClientTransport, createLogger } from '@contextvm/sdk';

Subpath Imports (Tree-shaking)

For smaller bundles, import directly from specific modules:

// Core utilities
import { createLogger } from '@contextvm/sdk/core';

// Transport implementations
import { NostrClientTransport } from '@contextvm/sdk/transport';

// Relay management
import { RelayPool } from '@contextvm/sdk/relay';

// Cryptographic signing
import { NostrSigner } from '@contextvm/sdk/signer';

// Gateway and Proxy
import { Gateway } from '@contextvm/sdk/gateway';
import { Proxy } from '@contextvm/sdk/proxy';

Note: Subpath imports help bundlers eliminate unused code. Use them when bundle size matters.

Usage

Visit the ContextVM documentation for information on how to use ContextVM.

Logging

The SDK uses Pino for high-performance logging with structured JSON output. By default, logs are written to stderr to comply with the MCP protocol expectations.

Basic Usage

import { createLogger } from '@contextvm/sdk/core';

// Create a logger for your module
const logger = createLogger('my-module');

logger.info('Application started');
logger.error('An error occurred', { error: 'details' });

Configuration Options

You can configure the logger with various options:

import { createLogger, LoggerConfig } from '@contextvm/sdk/core';

const config: LoggerConfig = {
  level: 'debug', // Minimum log level (debug, info, warn, error)
  file: 'app.log', // Optional: log to a file instead of stderr
};

const logger = createLogger('my-module', config);

Note: Pretty printing is automatically enabled when logs are written to stderr/stdout (not to a file) for better readability during development.

Configuring with Environment Variables

The logger can be configured using environment variables, which is useful for adjusting log output without changing the code.

  • LOG_LEVEL: Sets the minimum log level.
    • Values: debug, info, warn, error.
    • Default: info.
  • LOG_DESTINATION: Sets the log output destination.
    • Values: stderr (default), stdout, or file.
  • LOG_FILE: Specifies the file path when LOG_DESTINATION is file.
  • LOG_ENABLED: Enables or disables logging.
    • Values: true (default) or false.
Configuration in Node.js

Set the variables in your shell before running the application:

# Set log level to debug
LOG_LEVEL=debug node app.js

# Log to a file instead of the console
LOG_DESTINATION=file LOG_FILE=./app.log node app.js

# Disable logging entirely
LOG_ENABLED=false node app.js
Configuration in Browsers

In a browser environment, you can configure the log level by setting a global LOG_LEVEL variable on the window object before the SDK is imported or used.

// Set this in a <script> tag in your HTML or at the top of your entry point
window.LOG_LEVEL = 'debug';

// Now, when you import and use the SDK, it will use the 'debug' log level.
import { logger } from '@contextvm/sdk';
logger.debug('This is a debug message.');

Module-specific Loggers

Create child loggers for different modules to add context:

const baseLogger = createLogger('my-app');
const authLogger = baseLogger.withModule('auth');
const dbLogger = baseLogger.withModule('database');

authLogger.info('User login attempt');
dbLogger.debug('Query executed', { query: 'SELECT * FROM users' });

Development

This project requires Bun (version 1.2.0 or higher).

  1. Clone the repository:
git clone https://github.com/ContextVM/sdk.git
cd sdk
  1. Install dependencies:
bun install

Running Tests

To run the test suite, use Bun:

bun test

版本与发布记录

@contextvm/sdk 共发布 144 个版本,首次发布于 2025/07/15;过去 12 个月发布了 102 个版本。

版本发布日期距今
0.14.3latest2026/09/281 个月前
0.14.22026/09/251 个月前
0.14.12026/09/211 个月前
0.14.02026/09/201 个月前
0.13.172026/09/171 个月前
0.13.162026/09/111 个月前

其他发布通道:rc → 0.1.30-rc.1。

关于 @contextvm/sdk 的常见问题

@contextvm/sdk 是什么?

ContextVM SDK for JavaScript/TypeScript

如何安装 @contextvm/sdk?

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

@contextvm/sdk 支持 TypeScript 吗?

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

@contextvm/sdk 支持 ESM 还是 CommonJS?

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

@contextvm/sdk 可以商用吗?

@contextvm/sdk 使用 LGPL-3.0-1 许可。声明的许可为 LGPL-3.0-1,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@contextvm/sdk 的下载量有多少?

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

@contextvm/sdk 还在维护吗?

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