Token导航 LogoToken导航TokenDH.com
MCP Client Base logo
开发工具未说明官方级别未说明来源级核验

MCP Client Base

MCP Server

为VSCode扩展提供统一的模型上下文协议客户端基础功能,包括超时处理、自动重新同步和连接状态管理。

工具数

0

提示词数

0

GitHub Stars

0

资源数

0
VSCode扩展错误处理TypeScript

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

Digital-Defiance

提供方

Digital-Defiance

最后核验

2026/5/17 20:20

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

@人工智能功能套件/mcp客户群

共享MCP(模型上下文协议)客户端基类为VSCode扩展提供一致的超时处理、自动重新同步和连接状态管理。

特性

  • 可配置超时:初始化与标准请求的超时值不同
  • 自动重新同步:发生超时时的指数回退重试逻辑
  • 连接状态管理:跟踪并通知侦听器连接状态更改
  • 一致的错误处理:统一的错误消息和恢复选项
  • 可扩展架构:扩展可以自定义的抽象基类
  • 综合录井:带有时间戳和请求ID的结构化日志记录
  • 诊断命令:用于解决连接问题的内置命令

安装

npm install @ai-capabilities-suite/mcp-client-base

快速开始

import {
  BaseMCPClient,
  MCPClientConfig,
} from "@ai-capabilities-suite/mcp-client-base";
import * as vscode from "vscode";

// 1. Extend BaseMCPClient
export class MyMCPClient extends BaseMCPClient {
  protected getServerCommand() {
    return { command: "npx", args: ["-y", "@my-org/my-mcp-server"] };
  }

  protected getServerEnv() {
    return { ...process.env };
  }

  protected async onServerReady() {
    // Extension-specific initialization
  }
}

// 2. Create and start the client
const outputChannel = vscode.window.createOutputChannel("My Extension", {
  log: true,
});
const client = new MyMCPClient(outputChannel);
await client.start();

// 3. Use the client
const result = await client.callTool("my_tool", { param: "value" });

文档

关键概念

连接状态

客户端通过状态机跟踪连接状态:

  • DISCONNECTED -未连接到服务器
  • CONNECTING -正在尝试建立连接
  • CONNECTED -已成功连接并准备就绪
  • TIMEOUT_RETRYING -发生超时,正在尝试重新同步
  • ERROR -发生不可恢复的错误

超时处理

不同的请求类型具有不同的超时值:

  • 初始化:60秒(服务器启动可能很慢)
  • 标准请求:30秒(正常操作)
  • 工具列表:60秒(可能涉及发现)

重新同步

当初始化过程中发生超时时,客户端会自动尝试使用指数退避重新同步:

  1. 2秒后首次重试
  2. 3秒后第二次重试(2×1.5)
  3. 4.5秒后第三次重试(3×1.5)

使用示例

基本扩展

import { BaseMCPClient } from "@ai-capabilities-suite/mcp-client-base";
import * as vscode from "vscode";

export class MyMCPClient extends BaseMCPClient {
  constructor(outputChannel: vscode.LogOutputChannel) {
    super(outputChannel, {
      timeout: {
        initializationTimeoutMs: 60000,
        standardRequestTimeoutMs: 30000,
        toolsListTimeoutMs: 60000,
      },
      reSync: {
        maxRetries: 3,
        retryDelayMs: 2000,
        backoffMultiplier: 1.5,
      },
      logging: {
        logLevel: "info",
        logCommunication: true,
      },
    });
  }

  protected getServerCommand() {
    return {
      command: "npx",
      args: ["-y", "@my-org/my-mcp-server"],
    };
  }

  protected getServerEnv() {
    return { ...process.env };
  }

  protected async onServerReady() {
    // Verify server is working
    await this.callTool("health_check", {});
  }

  // Extension-specific methods
  async doSomething(params: any): Promise {
    return await this.callTool("my_tool", params);
  }
}

监控连接状态

const client = new MyMCPClient(outputChannel);

// Subscribe to state changes
const disposable = client.onStateChange((status) => {
  console.log(`State: ${status.state}`);
  console.log(`Message: ${status.message}`);
  console.log(`Server Running: ${status.serverProcessRunning}`);

  if (status.state === "ERROR") {
    vscode.window.showErrorMessage(`Connection error: ${status.message}`);
  }
});

await client.start();

// Later: cleanup
disposable.dispose();
client.stop();

使用诊断命令

import { diagnosticCommands } from "@ai-capabilities-suite/mcp-client-base";

// Register your extension
diagnosticCommands.registerExtension({
  name: "my-extension",
  displayName: "My Extension",
  client: myClient,
});

// Reconnect to server
await diagnosticCommands.reconnectToServer("my-extension");

// Restart server
await diagnosticCommands.restartServer("my-extension");

// Get diagnostics
const diag = diagnosticCommands.getDiagnostics("my-extension");
console.log(diagnosticCommands.formatDiagnostics(diag));

// Get all extensions status
const allDiag = diagnosticCommands.getAllDiagnostics();
console.log(diagnosticCommands.formatAllDiagnostics());

配置

默认配置

{
  timeout: {
    initializationTimeoutMs: 60000,  // 60 seconds
    standardRequestTimeoutMs: 30000,  // 30 seconds
    toolsListTimeoutMs: 60000,        // 60 seconds
  },
  reSync: {
    maxRetries: 3,                    // 3 retry attempts
    retryDelayMs: 2000,               // 2 second initial delay
    backoffMultiplier: 1.5,           // 1.5x backoff multiplier
  },
  logging: {
    logLevel: 'info',                 // info level logging
    logCommunication: true,           // log all communication
  },
}

自定义配置

const client = new MyMCPClient(outputChannel, {
  timeout: {
    initializationTimeoutMs: 120000, // 2 minutes for slow servers
    standardRequestTimeoutMs: 45000, // 45 seconds for slow operations
  },
  reSync: {
    maxRetries: 5, // More retry attempts
    retryDelayMs: 1000, // Faster initial retry
    backoffMultiplier: 2.0, // Aggressive backoff
  },
  logging: {
    logLevel: "debug", // Verbose logging
    logCommunication: true,
  },
});

建筑

该包由四个主要部分组成:

  1. BaseMCP客户端 -具有核心功能的抽象基类
  2. 超时管理器 -可配置的超时处理
  3. 连接状态管理器 -连接状态跟踪和通知
  4. 无效的管理器 -具有指数回退的自动重新同步
┌─────────────────────────────────────────────────────────────┐
│              @ai-capabilities-suite/mcp-client-base         │
│                                                              │
│  ┌────────────────────────────────────────────────────────┐ │
│  │              BaseMCPClient (Abstract)                   │ │
│  │  ┌──────────────┐  ┌────────────────┐  ┌───────────┐ │ │
│  │  │   Timeout    │  │ Re-sync Logic  │  │  Request  │ │ │
│  │  │   Manager    │  │                │  │   Queue   │ │ │
│  │  └──────────────┘  └────────────────┘  └───────────┘ │ │
│  │  ┌──────────────────────────────────────────────────┐ │ │
│  │  │        ConnectionStateManager                     │ │ │
│  │  └──────────────────────────────────────────────────┘ │ │
│  └────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
                            ▲
                            │ extends
          ┌─────────────────┼─────────────────┐
          │                 │                  │
┌─────────┴──────┐  ┌──────┴───────┐  ┌──────┴───────┐
│ MCPProcessClient│  │MCPScreenshot │  │MCPDebugger   │
│                 │  │   Client     │  │   Client     │
└─────────────────┘  └──────────────┘  └──────────────┘

测试

该包包括全面的测试:

  • 单元测试 -单独测试单个组件
  • 基于属性的测试 -验证所有输入的正确性属性
  • 集成测试 -测试完整的客户端生命周期

运行测试:

npm test

贡献

欢迎投稿!请确保:

  1. 所有测试均通过
  2. 新功能包括测试
  3. 文档已更新
  4. 代码遵循现有样式

许可证

麻省理工学院

目录标签

目录标签

VSCode扩展错误处理TypeScript本地部署客户端基础库连接管理自动重试

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

none

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明none部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP