Skip to main content

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
Responsibilities:
  • 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:
Key Principles:
  • 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)

The EbayApiClient 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)

The EbayOAuthClient 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
Token Priority:
  1. Valid user access token (if available)
  2. Refresh user token (if access token expired)
  3. App access token from client credentials (fallback)

Project Structure

Design Patterns

1. Facade Pattern

The EbaySellerApi class (src/api/index.ts) acts as a facade, providing a unified interface to all eBay API categories.
Benefits:
  • 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
Response Interceptor:
  • 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:
  1. User Token Strategy - Use valid user access token
  2. Token Refresh Strategy - Refresh expired user token
  3. 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.
This generates TypeScript types in 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.
Benefits:
  • 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 in src/config/environment.ts.
Environment Variables:
  • EBAY_CLIENT_ID - eBay application Client ID
  • EBAY_CLIENT_SECRET - eBay application Client Secret
  • EBAY_ENVIRONMENT - sandbox or production
  • EBAY_REDIRECT_URI - OAuth redirect URI (RuName)
  • EBAY_USER_ACCESS_TOKEN - Optional user access token
  • EBAY_USER_REFRESH_TOKEN - Optional user refresh token

Extensibility

The architecture is designed for easy extensibility:

Adding New eBay APIs

  1. Create API implementation in appropriate category directory
  2. Define Zod schemas for input validation
  3. Add TypeScript types (or generate from OpenAPI)
  4. Create tool definitions in src/tools/definitions/
  5. Register tools in tool registry
  6. 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 uses console.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)

Next Steps

Error Handling

Learn how errors are handled and recovered

Rate Limits

Understand rate limiting strategies

Testing

Explore the testing strategy and tools

Contributing

Contribute to the project