claude-task-master/docs/mcp-provider.md
Oren Me b53065713c
feat: add support for MCP Sampling as AI provider (#863)
* feat: support MCP sampling

* support provider registry

* use standard config options for MCP provider

* update fastmcp to support passing params to requestSampling

* move key name definition to base provider

* moved check for required api key to provider class

* remove unused code

* more cleanup

* more cleanup

* refactor provider

* remove not needed files

* more cleanup

* more cleanup

* more cleanup

* update docs

* fix tests

* add tests

* format fix

* clean files

* merge fixes

* format fix

* feat: add support for MCP Sampling as AI provider

* initial mcp ai sdk

* fix references to old provider

* update models

* lint

* fix gemini-cli conflicts

* ran format

* Update src/provider-registry/index.js

Co-authored-by: Ralph Khreish <35776126+Crunchyman-ralph@users.noreply.github.com>

* fix circular dependency

Circular Dependency Issue  FIXED
Root Cause: BaseAIProvider was importing from index.js, which includes commands.js and other modules that eventually import back to AI providers
Solution: Changed imports to use direct paths to avoid circular dependencies:
Updated base-provider.js to import log directly from utils.js
Updated gemini-cli.js to import log directly from utils.js
Result: Fixed 11 failing tests in mcp-provider.test.js

* fix gemini test

* fix(claude-code): recover from CLI JSON truncation bug (#913) (#920)

Gracefully handle SyntaxError thrown by @anthropic-ai/claude-code when the CLI truncates large JSON outputs (4–16 kB cut-offs).\n\nKey points:\n• Detect JSON parse error + existing buffered text in both doGenerate() and doStream() code paths.\n• Convert the failure into a recoverable 'truncated' finish state and push a provider-warning.\n• Allows Task Master to continue parsing long PRDs / expand-task operations instead of crashing.\n\nA patch changeset (.changeset/claude-code-json-truncation.md) is included for the next release.\n\nRef: eyaltoledano/claude-task-master#913

* docs: fix gemini-cli authentication documentation (#923)

Remove erroneous 'gemini auth login' command references and replace with correct 'gemini' command authentication flow. Update documentation to reflect proper OAuth setup process via the gemini CLI interactive interface.

* fix tests

* fix: update ai-sdk-provider-gemini-cli to 0.0.4 for improved authentication (#932)

- Fixed authentication compatibility issues with Google auth
- Added support for 'api-key' auth type alongside 'gemini-api-key'
- Resolved "Unsupported authType: undefined" runtime errors
- Updated @google/gemini-cli-core dependency to 0.1.9
- Improved documentation and removed invalid auth references
- Maintained backward compatibility while enhancing type validation

* call logging directly

Need to patch upstream fastmcp to allow easier access and bootstrap the TM mcp logger to use the fastmcp logger which today is only exposed in the tools handler

* fix tests

* removing logs until we figure out how to pass mcp logger

* format

* fix tests

* format

* clean up

* cleanup

* readme fix

---------

Co-authored-by: Oren Melamed <oren.m@gloat.com>
Co-authored-by: Ralph Khreish <35776126+Crunchyman-ralph@users.noreply.github.com>
Co-authored-by: Ben Vargas <ben@vargas.com>
2025-07-09 10:54:38 +02:00

9.4 KiB

MCP Provider Implementation

Overview

The MCP Provider creates a modern AI SDK-compliant custom provider that integrates with the existing Task Master MCP server infrastructure. This provider enables AI operations through MCP session sampling while following modern AI SDK patterns and includes full support for structured object generation (generateObject) for schema-driven features like PRD parsing and task creation.

Architecture

Components

  1. MCPProvider (mcp-server/src/providers/mcp-provider.js)

    • Main provider class following Claude Code pattern
    • Session-based provider (no API key required)
    • Registers with provider registry on MCP server connect
  2. AI SDK Implementation (mcp-server/src/custom-sdk/)

    • index.js - Provider factory function
    • language-model.js - LanguageModelV1 implementation with doGenerateObject support
    • message-converter.js - Format conversion utilities
    • json-extractor.js - NEW: Robust JSON extraction from AI responses
    • schema-converter.js - NEW: Schema-to-instructions conversion utility
    • errors.js - Error handling and mapping
  3. Integration Points

    • MCP Server registration (mcp-server/src/index.js)
    • AI Services integration (scripts/modules/ai-services-unified.js)
    • Model configuration (scripts/modules/supported-models.json)

Session Flow

MCP Client Connect → MCP Server → registerRemoteProvider()
                                        ↓
                           MCPRemoteProvider (existing)
                           MCPProvider 
                                        ↓
                               Provider Registry
                                        ↓
                               AI Services Layer
                                        ↓
                        Text Generation + Object Generation

Implementation Details

Provider Registration

The MCP server registers both providers when a client connects:

// mcp-server/src/index.js
registerRemoteProvider(session) {
  if (session?.clientCapabilities?.sampling) {
    // Register existing provider
    // Register unified MCP provider
    const mcpProvider = new MCPProvider();
    mcpProvider.setSession(session);
    
    const providerRegistry = ProviderRegistry.getInstance();
    providerRegistry.registerProvider('mcp', mcpProvider);
  }
}

AI Services Integration

The AI services layer includes the new provider:

// scripts/modules/ai-services-unified.js
const PROVIDERS = {
  // ... other providers
  'mcp': () => {
    const providerRegistry = ProviderRegistry.getInstance();
    return providerRegistry.getProvider('mcp');
  }
};

Message Conversion

The provider converts between AI SDK and MCP formats:

// AI SDK prompt → MCP sampling format
const { messages, systemPrompt } = convertToMCPFormat(options.prompt);

// MCP response → AI SDK format
const result = convertFromMCPFormat(response);

Structured Object Generation (generateObject)

Overview

The MCP Provider includes full support for structured object generation, enabling schema-driven features like PRD parsing, task creation, and any operations requiring validated JSON outputs.

Architecture

The generateObject implementation includes:

  1. Schema-to-Instructions Conversion (schema-converter.js)

    • Converts Zod schemas to natural language instructions
    • Generates example outputs to guide AI responses
    • Handles complex nested schemas and validation requirements
  2. JSON Extraction Pipeline (json-extractor.js)

    • Multiple extraction strategies for robust JSON parsing
    • Handles code blocks, malformed JSON, and various response formats
    • Fallback mechanisms for maximum reliability
  3. Validation System

    • Complete schema validation using Zod
    • Detailed error reporting for failed validations
    • Type-safe object generation

Implementation Details

doGenerateObject Method

The MCPLanguageModel class implements the AI SDK's doGenerateObject method:

async doGenerateObject({ schema, objectName, prompt, ...options }) {
  // Convert schema to instructions
  const instructions = convertSchemaToInstructions(schema, objectName);
  
  // Enhance prompt with structured output requirements
  const enhancedPrompt = enhancePromptForObjectGeneration(prompt, instructions);
  
  // Generate response via MCP sampling
  const response = await this.doGenerate({ prompt: enhancedPrompt, ...options });
  
  // Extract and validate JSON
  const extractedJson = extractJsonFromResponse(response.text);
  const validatedObject = schema.parse(extractedJson);
  
  return {
    object: validatedObject,
    usage: response.usage,
    finishReason: response.finishReason
  };
}

AI SDK Compatibility

The provider includes required properties for AI SDK object generation:

class MCPLanguageModel {
  get defaultObjectGenerationMode() {
    return 'tool';
  }
  
  get supportsStructuredOutputs() {
    return true;
  }
  
  // ... doGenerateObject implementation
}

Usage Examples

PRD Parsing

import { z } from 'zod';

const taskSchema = z.object({
  title: z.string(),
  description: z.string(),
  priority: z.enum(['high', 'medium', 'low']),
  dependencies: z.array(z.number()).optional()
});

const result = await generateObject({
  model: mcpModel,
  schema: taskSchema,
  prompt: 'Parse this PRD section into a task: [PRD content]'
});

console.log(result.object); // Validated task object

Task Creation

const taskCreationSchema = z.object({
  task: z.object({
    title: z.string(),
    description: z.string(),
    details: z.string(),
    testStrategy: z.string(),
    priority: z.enum(['high', 'medium', 'low']),
    dependencies: z.array(z.number()).optional()
  })
});

const result = await generateObject({
  model: mcpModel,
  schema: taskCreationSchema,
  prompt: 'Create a comprehensive task for implementing user authentication'
});

Error Handling

The implementation provides comprehensive error handling:

  • Schema Validation Errors: Detailed Zod validation messages
  • JSON Extraction Failures: Fallback strategies and clear error reporting
  • MCP Communication Errors: Proper error mapping and recovery
  • Timeout Handling: Configurable timeouts for long-running operations

Testing

The generateObject functionality is fully tested:

# Test object generation
npm test -- --grep "generateObject"

# Test with actual MCP session
node test-object-generation.js

Supported Features

Schema Conversion: Zod schemas → Natural language instructions
JSON Extraction: Multiple strategies for robust parsing
Validation: Complete schema validation with error reporting
Error Recovery: Fallback mechanisms for failed extractions
Type Safety: Full TypeScript support with inferred types
AI SDK Compliance: Complete LanguageModelV1 interface implementation

Usage

Configuration

Add to supported models configuration:

{
  "mcp": [
    {
      "id": "claude-3-5-sonnet-20241022",
      "swe_score": 0.623,
      "cost_per_1m_tokens": { "input": 0, "output": 0 },
      "allowed_roles": ["main", "fallback", "research"],
      "max_tokens": 200000
    }
  ]
}

CLI Usage

# Set provider for main role
tm models set-main --provider mcp --model claude-3-5-sonnet-20241022

# Use in task operations
tm add-task "Create user authentication system"

Programmatic Usage

const provider = registry.getProvider('mcp');
if (provider && provider.hasValidSession()) {
  const client = provider.getClient({ temperature: 0.7 });
  const model = client({ modelId: 'claude-3-5-sonnet-20241022' });
  
  const result = await model.doGenerate({
    prompt: [
      { role: 'user', content: 'Hello!' }
    ]
  });
}

Testing

Component Tests

# Test individual components
node test-mcp-components.js

Integration Testing

  1. Start MCP server
  2. Connect Claude client
  3. Verify both providers are registered
  4. Test AI operations through mcp provider

Validation Checklist

  • Provider creation and initialization
  • Registry integration
  • Session management
  • Message conversion
  • Error handling
  • AI Services integration
  • Model configuration

Key Benefits

  1. AI SDK Compliance - Full LanguageModelV1 implementation
  2. Session Integration - Leverages existing MCP session infrastructure
  3. Registry Pattern - Uses provider registry for discovery
  4. Backward Compatibility - Coexists with existing MCPRemoteProvider
  5. Future Ready - Supports AI SDK features and patterns

Troubleshooting

Provider Not Found

Error: Provider "mcp" not found in registry

Solution: Ensure MCP server is running and client is connected

Session Errors

Error: MCP Provider requires active MCP session

Solution: Check MCP client connection and session capabilities

Sampling Errors

Error: MCP session must have client sampling capabilities

Solution: Verify MCP client supports sampling operations

Next Steps

  1. Performance Optimization - Add caching and connection pooling
  2. Enhanced Streaming - Implement native streaming if MCP supports it
  3. Tool Integration - Add support for function calling through MCP tools
  4. Monitoring - Add metrics and logging for provider usage
  5. Documentation - Update user guides and API documentation