Architecture
The eBay MCP Server is built with a layered, modular architecture that prioritizes type safety, maintainability, and extensibility. This guide provides a deep understanding of how the server is structured and how its components interact.Overview
The server implements the Model Context Protocol (MCP) specification to expose eBay’s Sell APIs as tools that AI assistants can use. It follows a clean architecture pattern with clear separation of concerns.Core Layers
1. MCP Server Layer
The server layer implements the Model Context Protocol specification using the@modelcontextprotocol/sdk package.
Key Components:
- McpServer (src/index.ts:16) - Main MCP server instance that handles tool registration and execution
- StdioServerTransport (src/index.ts:104) - STDIO transport for communication with MCP clients
- Tool Registry - Dynamic registration of all 230+ eBay API tools
- Accept incoming tool requests from MCP clients
- Route requests to appropriate API implementations
- Format responses according to MCP specification
- Handle server lifecycle (startup, shutdown, error handling)
2. Business Logic Layer
The business logic layer contains domain-specific implementations of eBay APIs, organized by functional category. Directory Structure:- Each API category is self-contained with clear boundaries
- All methods use Zod schemas for input validation
- OpenAPI-generated types ensure type safety
- Consistent error handling across all APIs
3. Infrastructure Layer
The infrastructure layer provides cross-cutting concerns like HTTP communication, authentication, and rate limiting.HTTP Client (src/api/client.ts)
TheEbayApiClient class handles all HTTP communication with eBay APIs.
Features:
- Request/response interceptors for authentication
- Automatic token refresh on 401 errors
- Client-side rate limiting
- Exponential backoff retry logic for server errors
- Rate limit header tracking
OAuth Client (src/auth/oauth.ts)
TheEbayOAuthClient class manages OAuth 2.0 authentication with automatic token refresh.
Features:
- User token management with automatic refresh
- Client credentials flow (app tokens) as fallback
- Token expiry tracking
- Environment variable integration
- Valid user access token (if available)
- Refresh user token (if access token expired)
- App access token from client credentials (fallback)
Project Structure
Design Patterns
1. Facade Pattern
TheEbaySellerApi class (src/api/index.ts) acts as a facade, providing a unified interface to all eBay API categories.
- Single entry point for all eBay APIs
- Simplified dependency injection
- Easy to mock for testing
2. Interceptor Pattern
Axios interceptors handle cross-cutting concerns like authentication and error handling. Request Interceptor:- Inject authentication token
- Check rate limits before request
- Record request timestamp
- Extract rate limit headers
- Handle authentication errors (401)
- Implement retry logic for server errors (5xx)
- Transform eBay-specific errors
3. Strategy Pattern
The OAuth client uses a strategy pattern for token management, automatically selecting the best authentication method. Strategies:- User Token Strategy - Use valid user access token
- Token Refresh Strategy - Refresh expired user token
- Client Credentials Strategy - Fallback to app token
4. Repository Pattern
Each API category acts as a repository for its domain entities.Type Safety
The server implements multiple layers of type safety:1. OpenAPI-Generated Types
All eBay API request/response types are generated from OpenAPI specifications.src/types/sell_*.ts files.
2. Native TypeScript Enums
33+ native TypeScript enums provide compile-time type checking for eBay constants.3. Zod Runtime Validation
All external inputs (tool arguments, environment variables) are validated using Zod schemas.- Catch invalid inputs at runtime
- Automatic type inference
- Clear error messages
- Self-documenting code
Data Flow
Tool Execution Flow
Authentication Flow
Configuration Management
The server uses a centralized configuration system insrc/config/environment.ts.
EBAY_CLIENT_ID- eBay application Client IDEBAY_CLIENT_SECRET- eBay application Client SecretEBAY_ENVIRONMENT-sandboxorproductionEBAY_REDIRECT_URI- OAuth redirect URI (RuName)EBAY_USER_ACCESS_TOKEN- Optional user access tokenEBAY_USER_REFRESH_TOKEN- Optional user refresh token
Extensibility
The architecture is designed for easy extensibility:Adding New eBay APIs
- Create API implementation in appropriate category directory
- Define Zod schemas for input validation
- Add TypeScript types (or generate from OpenAPI)
- Create tool definitions in
src/tools/definitions/ - Register tools in tool registry
- Write tests for new functionality
Adding New Tool Categories
Performance Considerations
Rate Limit Management
The server implements client-side rate limiting to prevent exceeding eBay’s API limits:Connection Pooling
Axios automatically manages HTTP connection pooling for improved performance.Caching
Currently, the server does not implement response caching, as eBay data can change frequently. Consider implementing caching for specific use cases:- Metadata API responses (category trees, policies)
- Fulfillment/payment/return policies
- Rate limit headers
Security
Credential Management
- Credentials loaded from environment variables only
- Never logged or exposed in error messages
- Token storage in memory only (not persisted to disk)
Input Validation
- All inputs validated with Zod schemas
- SQL injection not applicable (REST API client)
- No user-generated code execution
HTTPS Only
All communication with eBay APIs uses HTTPS.Monitoring and Observability
Logging
The server usesconsole.error for logging to stderr:
- Server startup/shutdown events
- Authentication events (token refresh, expiry)
- Rate limit warnings
- Error conditions
Error Context
All errors include context for debugging:- Original eBay API error messages
- Suggested remediation steps
- Request/response details (in development)