Skip to content

fix: lazy-load MCP SDK to prevent build failures when SDK is not installed #396

Closed

Description

@admarble

Context

#372 (MCP server) added @modelcontextprotocol/sdk as a dependency and serve.ts imports it eagerly at the top of bin/cli.ts (line 53). This means every npx sequant command (run, doctor, init, etc.) loads the MCP SDK — even when the user has no intention of using MCP.

When the SDK isn't installed (or fails to resolve), this produces:

  • 14 TypeScript build errors on main
  • 5 test failures on main
  • Every feature branch that rebases/merges from main inherits the breakage
  • QA constantly has to distinguish "pre-existing on main" vs "introduced by this PR"

This caused friction during #369 (Aider) QA and merge.

Problem Files

File Import Type Impact
bin/cli.ts:53 import { serveCommand } from "../src/commands/serve.js" Eager — breaks all CLI commands
src/commands/serve.ts:10-11 import { StdioServerTransport, SSEServerTransport } Static — fails at import time
src/mcp/server.ts:9 import { McpServer } Static — fails at import time
src/mcp/tools/*.ts import type { McpServer } Type-only — OK at runtime, fails tsc
src/commands/doctor.ts:497 await import("../mcp/server.js") Dynamic — already handles failure correctly

Goal

Make the MCP SDK a truly optional dependency. npx sequant run and all non-MCP commands should work without the SDK installed.

Acceptance Criteria

  • bin/cli.ts lazy-loads serve.ts via dynamic import() inside the .action() handler
  • npx sequant run, npx sequant doctor, npx sequant init work without @modelcontextprotocol/sdk installed
  • npx sequant serve gives a clear error if SDK is not installed (not a cryptic import failure)
  • npm run build passes without the SDK installed (type-only imports need conditional handling)
  • Tests pass without the SDK installed (MCP tests can be skipped with a guard)
  • doctor MCP health check continues to work (already uses dynamic import — verify)

Design

CLI entry point (bin/cli.ts)

// Before (eager):
import { serveCommand } from "../src/commands/serve.js";
program.command("serve").action(serveCommand);

// After (lazy):
program.command("serve").action(async (...args) => {
  try {
    const { serveCommand } = await import("../src/commands/serve.js");
    return serveCommand(...args);
  } catch (err) {
    console.error("MCP server requires @modelcontextprotocol/sdk. Install: npm install @modelcontextprotocol/sdk");
    process.exit(1);
  }
});

Type-only imports

import type is erased at runtime but fails tsc. Options:

  1. Move MCP types to a local .d.ts file that mirrors the SDK types
  2. Use // @ts-ignore with a comment (not ideal)
  3. Make SDK a peerDependency with "optional": true

Option 3 is cleanest.

Testing

  • Remove @modelcontextprotocol/sdk from node_modules, verify all non-MCP commands work
  • Verify npx sequant serve gives a clear error
  • Verify npm run build passes

Activity

  1. added
    bug
    multi-agent
    on Mar 23, 2026
  2. admarble commented on Mar 23, 2026

    @admarble
    ContributorAuthor

    Spec Plan — #396: Lazy-load MCP SDK

    AC Checklist

    AC Description Verification
    AC-1 bin/cli.ts lazy-loads serve.ts via dynamic import() Code review + integration test
    AC-2 sequant run/doctor/init work without SDK installed Integration test (SDK removed)
    AC-3 sequant serve gives clear error without SDK Integration test
    AC-4 npm run build passes without SDK installed Build verification
    AC-5 Tests pass without SDK (MCP tests skipped) Test run with SDK removed
    AC-6 doctor MCP health check continues to work Verify existing dynamic import
    AC-7 import type statements resolve via ambient declarations Build verification

    Implementation Plan

    Phase 1 — Lazy-load serve command (AC-1, AC-2, AC-3)

    1. Remove static import { serveCommand } from bin/cli.ts
    2. Replace .action(serveCommand) with async wrapper using await import()
    3. Catch import errors → show "MCP requires @modelcontextprotocol/sdk" message

    Phase 2 — Ambient type declarations (AC-4, AC-7)
    4. Create src/mcp/sdk-types.d.ts with ambient module declarations for SDK types
    5. Move @modelcontextprotocol/sdk from dependencies to peerDependencies (optional)

    Phase 3 — Test guards (AC-5)
    6. Add describe.skipIf() guard to server.test.ts and server-extended.test.ts

    Phase 4 — Verify (AC-6)
    7. Confirm doctor.ts dynamic import still works (no changes needed)

    Non-Goals

    • Not removing MCP functionality
    • Not refactoring MCP internals (tools, resources, server logic)
    • Not changing MCP server API surface

    Scope

    ✅ SCOPE_OK — Single focused fix, 3 directories, 6 AC items

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled

  3. added
    planned
    on Mar 23, 2026
  4. added a commit that references this issue on Mar 23, 2026
  5. admarble commented on Mar 23, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #396
    Status: READY_FOR_MERGE

    AC Coverage

    AC Description Status
    AC-1 bin/cli.ts lazy-loads serve.ts via dynamic import() MET
    AC-2 sequant run/doctor/init work without SDK installed MET
    AC-3 sequant serve gives clear error if SDK missing MET
    AC-4 npm run build passes without SDK installed MET
    AC-5 Tests pass without SDK (MCP tests skipped) MET
    AC-6 doctor MCP health check continues to work MET

    Key Changes

    • bin/cli.ts: Removed static import, added lazy-load in .action() handler
    • src/mcp/sdk-types.d.ts: New ambient type declarations as tsc fallback
    • package.json: Moved SDK from dependencies to optional peerDependencies
    • 3 MCP test files: Added describe.skipIf(!mcpSdkAvailable) guards
    • eslint.config.js: Added *.d.ts to ignores

    Iterations

    Phase Iterations Status
    Spec 1/1 Complete
    Exec 1/1 Complete
    QA 1/2 Complete

    Quality Metrics

    • Build: Passes with and without SDK
    • Tests: 1663 passed (with SDK), 1628 passed + 3 skipped (without SDK)
    • Lint: 0 errors, 0 warnings

    PR: #398


    Ready for human review and merge.

  6. added 2 commits that reference this issue on Mar 23, 2026
  7. admarble commented on Mar 23, 2026

    @admarble
    ContributorAuthor

    Documentation Updated

    File: docs/features/mcp-server.md (updated existing doc)

    Changes:

    • Added @modelcontextprotocol/sdk to prerequisites with note that it's optional
    • Added troubleshooting entry for "MCP server requires @modelcontextprotocol/sdk" error

    No new doc file needed — the existing MCP server documentation was updated to reflect the SDK being an optional peer dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmulti-agentplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions