Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
385 changes: 385 additions & 0 deletions docs/polymarket-integration-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
# Polymarket Integration: Development & Testing Plan

## Overview

This document outlines the structured approach for building and testing a Polymarket integration for the MCP Connectors project. Polymarket is a prediction market platform where users can trade on real-world event outcomes.

## 1. Research & Planning Phase

### 1.1 Understanding Polymarket's Domain

**Core Concepts:**
- **Prediction Markets**: Markets where users bet on event outcomes
- **Markets**: Trading venues for specific questions/events
- **Outcomes**: Possible results (Yes/No, multiple choice)
- **Positions**: User holdings in specific outcomes
- **Liquidity**: Available trading volume
- **Settlement**: How markets resolve and payouts occur

**Key Use Cases:**
- Browse active prediction markets
- Get detailed market information and current probabilities
- Search for specific markets by topic
- View trending markets
- Check user portfolio and positions (with API key)
- View trading history (with API key)

### 1.2 API Research Requirements

**Before Implementation:**
1. **Study Polymarket's API documentation** (if available)
2. **Identify the actual API endpoints** and their structure
3. **Understand authentication methods** (API keys, OAuth, etc.)
4. **Map out data models** for markets, outcomes, positions, trades
5. **Test API endpoints** manually to understand response formats

**Key Questions to Answer:**
- What's the base URL for Polymarket's API?
- What authentication method does Polymarket use?
- What are the actual endpoint paths and parameters?
- How are probabilities and prices represented?
- What's the rate limiting structure?
- Are there different permission levels (public vs private data)?

## 2. Implementation Structure

### 2.1 File Organization

```
packages/mcp-connectors/src/connectors/
├── polymarket.ts # Main connector implementation
├── polymarket.spec.ts # Comprehensive test suite
└── __mocks__/
└── context.ts # Mock context for testing
```

### 2.2 Code Structure (Following Established Patterns)

**1. Type Definitions:**
```typescript
interface PolymarketMarket {
id: string;
question: string;
outcomes: PolymarketOutcome[];
liquidity: number;
status: 'open' | 'closed' | 'settled';
// ... other fields
}
```

**2. API Client Class:**
```typescript
class PolymarketClient {
constructor(apiKey?: string) { /* ... */ }

async getMarkets(limit?: number, category?: string): Promise<PolymarketMarket[]>
async getMarket(marketId: string): Promise<PolymarketMarket>
async searchMarkets(query: string): Promise<PolymarketMarket[]>
// ... other methods
}
```

**3. Connector Configuration:**
```typescript
export const PolymarketConnectorConfig = mcpConnectorConfig({
name: 'Polymarket',
key: 'polymarket',
// ... configuration
tools: (tool) => ({
GET_MARKETS: tool({ /* ... */ }),
GET_MARKET: tool({ /* ... */ }),
// ... other tools
}),
});
```

### 2.3 Tools to Implement

**Public Data Tools (No API Key Required):**
1. `polymarket_get_markets` - List markets with optional filtering
2. `polymarket_get_market` - Get detailed market information
3. `polymarket_search_markets` - Search markets by keyword
4. `polymarket_get_trending_markets` - Get trending markets

**Private Data Tools (API Key Required):**
5. `polymarket_get_user_profile` - User balance and portfolio summary
6. `polymarket_get_user_positions` - Current positions across markets
7. `polymarket_get_trade_history` - Recent trading activity

## 3. Testing Strategy

### 3.1 Test Structure (Following Project Standards)

**File: `polymarket.spec.ts`**

**Test Organization:**
```typescript
describe('#PolymarketConnector', () => {
describe('.GET_MARKETS', () => {
describe('when API call succeeds', () => {
describe('and markets are returned', () => {
it('returns formatted markets list', async () => {
// Test implementation
});
});

describe('and no markets are returned', () => {
it('returns no markets message', async () => {
// Test implementation
});
});
});

describe('when API call fails', () => {
it('returns error message', async () => {
// Test implementation
});
});
});

// ... similar structure for all tools
});
```

### 3.2 Test Coverage Requirements

**For Each Tool:**
- ✅ **Happy Path**: Successful API calls with valid data
- ✅ **Empty Results**: API returns empty arrays/objects
- ✅ **Error Handling**: API errors, network failures
- ✅ **Input Validation**: Invalid parameters, missing required fields
- ✅ **Authentication**: API key required vs optional scenarios

**Specific Test Cases:**
1. **GET_MARKETS**: Test with/without category filter, different limits
2. **GET_MARKET**: Test with valid/invalid market IDs
3. **SEARCH_MARKETS**: Test with various search queries
4. **User Tools**: Test with/without API key, various user states

### 3.3 Mocking Strategy

**Use MSW (Mock Service Worker) for API mocking:**
```typescript
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';

const server = setupServer(
http.get('https://api.polymarket.com/v1/markets', () => {
return HttpResponse.json({
data: [/* mock market data */]
});
})
);
```

**Mock Context:**
```typescript
const mockContext = createMockConnectorContext({
apiKey: 'test-api-key',
});
```

## 4. Development Workflow

### 4.1 Step-by-Step Implementation

**Phase 1: Basic Structure**
1. ✅ Create `polymarket.ts` with type definitions
2. ✅ Implement `PolymarketClient` class with basic methods
3. ✅ Create connector configuration with placeholder tools
4. ✅ Add to main index exports

**Phase 2: Core Functionality**
1. 🔄 Implement public data tools (GET_MARKETS, GET_MARKET, etc.)
2. 🔄 Add proper error handling and input validation
3. 🔄 Create formatting functions for LLM consumption
4. 🔄 Test with real API endpoints (if available)

**Phase 3: User-Specific Features**
1. ⏳ Implement private data tools (user profile, positions, trades)
2. ⏳ Add authentication handling
3. ⏳ Test with API key scenarios

**Phase 4: Testing & Polish**
1. ⏳ Write comprehensive test suite
2. ⏳ Add edge case handling
3. ⏳ Performance optimization
4. ⏳ Documentation updates

### 4.2 Debugging Strategy

**Local Development:**
1. **Use the testing agent** to test the connector locally
2. **Add console.log statements** for debugging API responses
3. **Test with real Polymarket API** (if available)
4. **Use browser dev tools** to inspect network requests

**Error Handling:**
```typescript
try {
const result = await client.getMarkets(args.limit);
return formatMarketsList(result);
} catch (error) {
console.error('Polymarket API error:', error);
return `Failed to get markets: ${error instanceof Error ? error.message : String(error)}`;
}
```

## 5. Integration Points

### 5.1 Adding to Main Exports

**Update `packages/mcp-connectors/src/index.ts`:**
```typescript
import { PolymarketConnectorConfig } from './connectors/polymarket';

export const Connectors: readonly MCPConnectorConfig[] = [
// ... existing connectors
PolymarketConnectorConfig,
// ... rest of connectors
];

export {
// ... existing exports
PolymarketConnectorConfig,
// ... rest of exports
};
```

### 5.2 Configuration Requirements

**Credentials Schema:**
```typescript
credentials: z.object({
apiKey: z
.string()
.optional()
.describe('Polymarket API Key (optional for public data, required for user-specific data)'),
}),
```

**Setup Schema:**
```typescript
setup: z.object({}),
```

## 6. Quality Assurance

### 6.1 Code Quality Checks

**Before Submission:**
1. ✅ Run `npm run check` for linting
2. ✅ Run `npm run test` for all tests
3. ✅ Run `npm run build` to ensure compilation
4. ✅ Test with the testing agent locally

### 6.2 Testing Checklist

**Functional Testing:**
- [ ] All tools work with valid inputs
- [ ] Error handling works for invalid inputs
- [ ] API key requirements are enforced correctly
- [ ] Response formatting is consistent and readable
- [ ] Rate limiting is handled gracefully

**Integration Testing:**
- [ ] Connector loads without errors
- [ ] All tools are accessible through MCP server
- [ ] Credentials are handled correctly
- [ ] Error messages are user-friendly

## 7. Future Enhancements

### 7.1 Potential Additional Features

**Advanced Trading Features:**
- Place buy/sell orders (if API supports)
- Get order book depth
- Real-time price updates
- Market alerts and notifications

**Analytics Features:**
- Market performance metrics
- User trading statistics
- Portfolio analytics
- Risk assessment tools

**Social Features:**
- Market comments/discussions
- User reputation systems
- Market creation (if supported)

### 7.2 Performance Optimizations

**Caching Strategy:**
- Cache market data for short periods
- Implement request deduplication
- Use pagination for large datasets

**Error Recovery:**
- Implement retry logic for failed requests
- Add circuit breaker patterns
- Graceful degradation for partial failures

## 8. Documentation

### 8.1 User Documentation

**Example Prompts:**
- "Show me trending prediction markets"
- "Get details about the Bitcoin price prediction market"
- "Search for markets related to the 2024 election"
- "What are my current positions and P&L?"

**API Reference:**
- Document all tool parameters
- Provide example responses
- Explain authentication requirements
- List common error scenarios

### 8.2 Developer Documentation

**Implementation Notes:**
- API endpoint documentation
- Data model explanations
- Error handling patterns
- Testing strategies

## 9. Deployment & Monitoring

### 9.1 Production Considerations

**Environment Variables:**
- API rate limits
- Timeout configurations
- Logging levels

**Monitoring:**
- API response times
- Error rates
- Usage patterns
- Rate limit usage

### 9.2 Rollout Strategy

**Phases:**
1. **Alpha**: Internal testing with mock data
2. **Beta**: Limited external testing with real API
3. **Production**: Full release with monitoring

**Rollback Plan:**
- Feature flags for gradual rollout
- Monitoring alerts for issues
- Quick rollback procedures

---

## Next Steps

1. **Research Polymarket's actual API** and update the implementation accordingly
2. **Implement the basic structure** following the established patterns
3. **Write comprehensive tests** for all scenarios
4. **Test with real API endpoints** when available
5. **Iterate based on feedback** and real-world usage

This plan provides a structured approach to building a robust Polymarket integration that follows the project's established patterns and quality standards.
Loading