tests — mcp

Module: tests-mcp Cohesion: 0.80 Members: 0

tests — mcp

The tests/mcp module contains comprehensive unit and integration tests for the Model Context Protocol (MCP) client and server implementations within the CodeBuddy project. These tests are crucial for ensuring the reliability, correctness, and adherence to the MCP specification for both interacting with external MCP servers and exposing CodeBuddy's internal capabilities as an MCP server.

This documentation will cover:

Purpose and Scope

The tests in this module validate the core functionality of CodeBuddy's MCP integration:

  1. Client-side Interaction (client.test.ts): Ensures CodeBuddy can correctly connect to, manage, and interact with external MCP servers using both the modern SDK-based MCPManager and the legacy MCPClient. This includes server lifecycle, tool discovery, tool execution, error handling, and health checks.
  2. Server-side Exposure (mcp-server.test.ts, mcp-agent-server.test.ts): Verifies that CodeBuddyMCPServer correctly registers and exposes CodeBuddy's internal tools (e.g., file editing, Git, Bash), agent intelligence capabilities (e.g., chat, task planning), memory, sessions, and project context as MCP tools, resources, and prompts. It also validates the server's lifecycle and the schemas of its exposed APIs.

These tests collectively guarantee that CodeBuddy can seamlessly integrate with the MCP ecosystem, both as a consumer of external tools and as a provider of its own intelligent services.

MCP Client Tests (client.test.ts)

client.test.ts focuses on testing how CodeBuddy interacts with external MCP servers. It covers two distinct client implementations: MCPManager (the recommended SDK-based approach) and MCPClient (a legacy, manual client).

MCPManager (SDK-based Client)

The MCPManager is designed to manage multiple MCP server connections using the @modelcontextprotocol/sdk/client library. Its tests cover:

MCPClient (Legacy Manual Client)

The MCPClient represents an older, more manual way of interacting with MCP servers, primarily via stdio transport and child_process. Its tests validate:

Common Mocking Strategies

client.test.ts heavily relies on mocking to isolate the client logic:

MCP Server Tests (mcp-server.test.ts, mcp-agent-server.test.ts)

These two test files collectively validate the CodeBuddyMCPServer, which exposes CodeBuddy's internal capabilities as an MCP server.

CodeBuddyMCPServer (Core Tools - mcp-server.test.ts)

mcp-server.test.ts focuses on the fundamental aspects of CodeBuddyMCPServer, particularly the registration and execution of its core development tools.

CodeBuddyMCPServer (Agent & Intelligence Layer - mcp-agent-server.test.ts)

mcp-agent-server.test.ts extends the server testing to cover the integration of CodeBuddy's AI agent, memory, session management, and web search capabilities as MCP tools, resources, and prompts.

Common Mocking Strategies

Both server test files employ extensive mocking:

Architecture Overview of CodeBuddyMCPServer Registrations

The CodeBuddyMCPServer acts as a facade, registering various CodeBuddy functionalities with the underlying MCP SDK.

graph TD
    subgraph CodeBuddyMCPServer
        A[MCP Server Instance] --> B(Registers Tools)
        A --> C(Registers Resources)
        A --> D(Registers Prompts)
    end

    subgraph Tool Categories
        B --> B1(Text Editor Tools)
        B --> B2(Search Tools)
        B --> B3(Git Tools)
        B --> B4(Bash Tool)
        B --> B5(Agent Tools)
        B --> B6(Memory Tools)
        B --> B7(Session Tools)
        B --> B8(Web Search Tool)
    end

    subgraph Resource Categories
        C --> C1(Project Context)
        C --> C2(Project Instructions)
        C --> C3(Latest Session)
        C --> C4(All Memories)
    end

    subgraph Prompt Categories
        D --> D1(Code Review)
        D --> D2(Explain Code)
        D --> D3(Generate Tests)
        D --> D4(Refactor)
        D --> D5(Fix Bugs)
    end

This diagram illustrates how the CodeBuddyMCPServer orchestrates the exposure of CodeBuddy's capabilities through the MCP. Each box represents a category of functionality that is registered with the MCP server, making it discoverable and callable by MCP clients.

Relationship to Production Code

These tests directly validate the behavior of the following production modules:

By thoroughly testing these components, the tests/mcp module ensures that CodeBuddy's integration with the Model Context Protocol is robust, functional, and correctly exposes its powerful features to other MCP-compatible clients.