This document outlines the detailed plan for refactoring the MCP Sampling provider to follow the custom AI SDK pattern, as exemplified by the claude-code provider.
We will start by creating the following directory and file structure:
src/
└── ai-providers/
├── custom-sdk/
│ └── mcp-sampling/
│ ├── index.js
│ ├── language-model.js
│ ├── message-converter.js
│ ├── errors.js
│ └── types.js
└── mcp-sampling.js
This file will define the JSDoc types for the MCP Sampling provider. This will include types for:
McpSamplingSettings: Configuration options for the provider.McpSamplingModelId: The model IDs supported by the provider.McpSamplingErrorMetadata: Metadata for custom errors.McpSamplingProvider: The provider interface.McpSamplingProviderSettings: Settings for the provider factory.
This file will create custom error classes for the provider, including:
createApiCallError: For general API errors.createAuthenticationError: For authentication-related errors.createTimeoutError: For timeout errors.
This file will contain the logic to convert the AI SDK prompt format to the format expected by the MCP Sampling service. It will export a convertToMcpSamplingMessages function.
This file will contain the core logic of the provider in the McpSamplingLanguageModel class. This class will:
- Implement the
doGeneratemethod for text and object generation. - Implement the
doStreammethod for streaming responses. - Handle authentication and session management.
- Manage the connection to the MCP Sampling service.
- Properly handle errors and warnings.
This file will be the entry point for the custom SDK. It will:
- Export a
createMcpSamplingfactory function that creates an instance of theMcpSamplingLanguageModel. - Export the
McpSamplingLanguageModelclass. - Export the error handling functions from
errors.js.
This file will be the main provider file that integrates with the rest of the application. It will:
- Import
createMcpSamplingfrom the custom SDK. - Export a
McpSamplingProviderclass that extendsBaseAIProvider. - Implement the
getClientmethod to return a newMcpSamplingLanguageModelinstance.
We will create a comprehensive suite of tests for the new provider:
- Unit Tests: In
tests/unit/ai-providers/, we will createmcp-sampling.test.jsto test the individual components of the provider, including the language model, message converter, and error handling. - Integration Tests: In
tests/integration/, we will create tests to verify the provider's interaction with a mock MCP Sampling service. This will ensure that the provider can correctly handle requests and responses.
Once the new provider is implemented and tested, we will perform the following steps to migrate to the new implementation:
- Update
src/ai-providers/index.jsto export the newMcpSamplingProvider. - Remove the old MCP Sampling provider implementation from the codebase.
- Update any code that uses the old provider to use the new provider.
- Run all tests to ensure that the migration was successful.