MCP Context7
MCP Context7
Section titled “MCP Context7”Overview and Purpose
Section titled “Overview and Purpose”Context7 is a comprehensive framework for managing contextual information across seven critical dimensions in software applications. It provides a structured approach to capturing, maintaining, and utilizing the full context of user interactions, system states, and application behaviors.
The Context7 MCP (Master Control Program) component enables developers to:
- Maintain rich, multi-dimensional context throughout application lifecycles
- Enhance user experiences with personalized, context-aware interactions
- Improve debugging and error handling with comprehensive contextual information
- Enable sophisticated AI-assisted features with rich contextual awareness
- Support collaborative environments with shared contextual understanding
- Provide continuity across sessions, devices, and user journeys
By implementing Context7, applications gain a “memory” that extends beyond simple state management, capturing the full richness of context that humans naturally utilize in their interactions.
The 7 Dimensions of Context
Section titled “The 7 Dimensions of Context”Context7 organizes contextual information into seven distinct but interconnected dimensions:
1. Temporal Context
Section titled “1. Temporal Context”Temporal context captures time-based information and historical patterns:
- Timestamps of user actions and system events
- Chronological action history
- Usage patterns over time
- Frequency analysis of interactions
- Duration of tasks and processes
- Session timing information
interface TemporalContext { timestamp: number; // Current timestamp sessionStartTime: number; // When the current session began actionHistory: Array<{ // Record of user actions action: string; // What action was performed timestamp: number; // When it occurred metadata?: any; // Additional action-specific data }>; frequencyMap: Record<string, number>; // Action frequency counts durations: Record<string, number>; // Task/process durations}2. Spatial Context
Section titled “2. Spatial Context”Spatial context tracks location-based information in physical or virtual spaces:
- Physical location (coordinates, regions)
- Virtual positioning (routes, pages, views)
- Proximity relationships
- Navigation paths and history
- Location hierarchies
- Spatial anchors and landmarks
interface SpatialContext { physicalLocation?: { // Physical location (if available) coordinates?: { latitude: number; longitude: number; }; region?: string; // Named region accuracy?: number; // Accuracy in meters }; virtualLocation: { // Virtual location currentRoute: string; // Current application route/page previousRoute: string; // Previous route navigationStack: string[]; // Navigation history activeView: string; // Currently active view component activeDialogs: string[]; // Open dialogs/modals }; proximity?: Record<string, number>; // Distances to important locations}3. State Context
Section titled “3. State Context”State context represents the current application state and configuration:
- Data structures and objects
- Application configuration
- Feature flags and toggles
- System status indicators
- Resource availability
- Process states and conditions
interface StateContext { applicationState: Record<string, any>; // Key-value state store configuration: Record<string, any>; // App configuration featureFlags: Record<string, boolean>; // Enabled/disabled features systemStatus: { // System status online: boolean; // Network connectivity performance: 'high' | 'medium' | 'low'; // Performance level resources: Record<string, number>; // Resource utilization }; processState: Record<string, string>; // States of ongoing processes}4. Semantic Context
Section titled “4. Semantic Context”Semantic context captures meaning, relationships, and conceptual understanding:
- Entities and their attributes
- Relationships between entities
- Topics and themes
- Taxonomies and categorizations
- Semantic tags and labels
- Knowledge graphs and ontologies
interface SemanticContext { entities: Map<string, { // Named entities type: string; // Entity type attributes: Record<string, any>; // Entity attributes relationships?: Record<string, string[]>; // Related entities }>; topics: Map<string, number>; // Active topics with relevance scores tags: string[]; // Semantic tags taxonomies: Record<string, string[]>; // Categorization systems knowledgeGraph?: any; // Structured knowledge representation}5. User Context
Section titled “5. User Context”User context contains user-specific information and personalization:
- User identity and profile
- Preferences and settings
- Expertise and proficiency levels
- Permissions and access rights
- Behavioral patterns
- Goals and intentions
interface UserContext { userId: string; // Unique user identifier profile: { // User profile name?: string; // User name email?: string; // User email role?: string; // User role expertise: 'beginner' | 'intermediate' | 'advanced'; // Skill level preferences: Record<string, any>; // User preferences }; authStatus: 'anonymous' | 'authenticated' | 'verified'; // Authentication status permissions: string[]; // User permissions behaviors: { // Observed behavioral patterns preferences: Record<string, any>; // Inferred preferences patterns: Record<string, any>; // Usage patterns }; goals?: string[]; // Current user goals}6. Task Context
Section titled “6. Task Context”Task context focuses on goals, tasks, and activities:
- Current tasks and objectives
- Task status and progress
- Dependencies and prerequisites
- Task history and completion rates
- Workflows and sequences
- Task metadata and properties
interface TaskContext { currentTask?: { // Currently active task id: string; // Task identifier description: string; // Task description status: 'pending' | 'in_progress' | 'completed' | 'failed'; // Task status progress: number; // Completion percentage (0-100) startTime?: number; // When task was started deadline?: number; // Task deadline metadata?: Record<string, any>; // Additional task data }; taskStack: Array<{ // Stack of tasks (for nested tasks) id: string; description: string; }>; completedTasks: string[]; // Recently completed task IDs taskGraph?: Record<string, string[]>; // Task dependencies workflows: Record<string, { // Defined workflows steps: string[]; // Workflow steps currentStep?: string; // Current step in workflow }>;}7. Environmental Context
Section titled “7. Environmental Context”Environmental context captures system and technical environment details:
- Device characteristics
- Browser/runtime information
- Network conditions
- Operating system details
- Screen/viewport properties
- Accessibility settings
interface EnvironmentalContext { device: { // Device information type: 'desktop' | 'tablet' | 'mobile' | 'other'; orientation?: 'portrait' | 'landscape'; screenSize: { width: number; height: number; }; touchEnabled: boolean; }; browser?: { // Browser information (if applicable) name: string; version: string; language: string; cookiesEnabled: boolean; }; operatingSystem: { // OS information name: string; version: string; platform: string; }; network: { // Network conditions type?: string; // Connection type speed?: 'slow' | 'medium' | 'fast'; // Connection speed latency?: number; // Latency in ms }; accessibility: { // Accessibility settings reducedMotion: boolean; highContrast: boolean; screenReader: boolean; fontSize: number; };}Implementation Guidelines
Section titled “Implementation Guidelines”To implement Context7 in your applications, follow these guidelines:
Core Context Manager
Section titled “Core Context Manager”Create a central Context7Manager class to manage all context dimensions:
class Context7Manager { private temporalContext: TemporalContext; private spatialContext: SpatialContext; private stateContext: StateContext; private semanticContext: SemanticContext; private userContext: UserContext; private taskContext: TaskContext; private environmentalContext: EnvironmentalContext;
constructor(initialContext?: { temporal?: Partial<TemporalContext>; spatial?: Partial<SpatialContext>; state?: Partial<StateContext>; semantic?: Partial<SemanticContext>; user?: Partial<UserContext>; task?: Partial<TaskContext>; environmental?: Partial<EnvironmentalContext>; }) { // Initialize with defaults and any provided values this.temporalContext = { timestamp: Date.now(), sessionStartTime: Date.now(), actionHistory: [], frequencyMap: {}, durations: {}, ...initialContext?.temporal };
this.spatialContext = { virtualLocation: { currentRoute: '/', previousRoute: '', navigationStack: ['/'], activeView: 'home', activeDialogs: [] }, ...initialContext?.spatial };
// Initialize other context dimensions... // (similar pattern for other dimensions) }
// Methods to get individual context dimensions getTemporalContext(): TemporalContext { return this.temporalContext; }
getSpatialContext(): SpatialContext { return this.spatialContext; }
// Getters for other dimensions...
// Get full context (all dimensions) getFullContext() { return { temporal: this.temporalContext, spatial: this.spatialContext, state: this.stateContext, semantic: this.semanticContext, user: this.userContext, task: this.taskContext, environmental: this.environmentalContext }; }
// Update specific context dimensions updateTemporalContext(update: Partial<TemporalContext>) { this.temporalContext = { ...this.temporalContext, ...update, timestamp: Date.now() // Always update timestamp }; }
// Update methods for other dimensions...
// Record a user action (updates temporal context) recordUserAction(action: string, metadata?: any) { const actionRecord = { action, timestamp: Date.now(), metadata };
// Add to action history this.temporalContext.actionHistory.push(actionRecord);
// Update frequency map this.temporalContext.frequencyMap[action] = (this.temporalContext.frequencyMap[action] || 0) + 1;
// Update virtual location if it's a navigation action if (action.startsWith('navigate_to_')) { const route = action.replace('navigate_to_', ''); this.updateSpatialContext({ virtualLocation: { ...this.spatialContext.virtualLocation, previousRoute: this.spatialContext.virtualLocation.currentRoute, currentRoute: route, navigationStack: [ ...this.spatialContext.virtualLocation.navigationStack, route ] } }); }
return actionRecord; }
// Serialize context to string serialize(): string { return JSON.stringify(this.getFullContext()); }
// Create Context7Manager from serialized string static deserialize(serialized: string): Context7Manager { try { const parsed = JSON.parse(serialized); return new Context7Manager(parsed); } catch (error) { console.error('Failed to deserialize context:', error); return new Context7Manager(); } }}Context Providers and Consumers
Section titled “Context Providers and Consumers”For frameworks like React, create context providers and consumers:
// React context exampleimport React, { createContext, useContext, useState, useEffect } from 'react';
// Create Context7 React contextconst Context7Context = createContext<{ context: Context7Manager; updateContext: (dimension: string, update: any) => void; recordAction: (action: string, metadata?: any) => void;}>({ context: new Context7Manager(), updateContext: () => {}, recordAction: () => {}});
// Context provider componentexport function Context7Provider({ children }) { const [context7] = useState(() => new Context7Manager());
// Update context timestamp periodically useEffect(() => { const interval = setInterval(() => { context7.updateTemporalContext({ timestamp: Date.now() }); }, 30000); // every 30 seconds
return () => clearInterval(interval); }, [context7]);
// Detect environment on mount useEffect(() => { const detectEnvironment = () => { // Logic to detect browser, OS, screen size, etc. // ...
return { device: { type: detectDeviceType(), screenSize: { width: window.innerWidth, height: window.innerHeight }, touchEnabled: 'ontouchstart' in window }, // Other environment detection... }; };
context7.updateEnvironmentalContext(detectEnvironment());
// Update on resize const handleResize = () => { context7.updateEnvironmentalContext({ device: { ...context7.getEnvironmentalContext().device, screenSize: { width: window.innerWidth, height: window.innerHeight } } }); };
window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, [context7]);
// Create context update helpers const updateContext = (dimension: string, update: any) => { switch (dimension) { case 'temporal': context7.updateTemporalContext(update); break; case 'spatial': context7.updateSpatialContext(update); break; // Other dimensions... default: console.warn(`Unknown context dimension: ${dimension}`); } };
// Action recorder const recordAction = (action: string, metadata?: any) => { return context7.recordUserAction(action, metadata); };
return ( <Context7Context.Provider value={{ context: context7, updateContext, recordAction }}> {children} </Context7Context.Provider> );}
// Custom hook for consuming contextexport function useContext7() { return useContext(Context7Context);}
// Usage example in a componentfunction UserDashboard() { const { context, recordAction } = useContext7();
useEffect(() => { // Record that user visited dashboard recordAction('view_dashboard'); }, [recordAction]);
// Use context to personalize dashboard const userContext = context.getUserContext();
return ( <div> <h1>Welcome, {userContext.profile.name || 'User'}</h1> {/* Personalized dashboard content */} </div> );}Context Persistence
Section titled “Context Persistence”Implement persistence to maintain context across sessions:
// Example persistence implementationclass PersistentContext7 { private context: Context7Manager; private storageKey: string;
constructor(userId: string) { this.storageKey = `context7-${userId}`; this.context = this.loadContext() || new Context7Manager(); }
private loadContext(): Context7Manager | null { try { const stored = localStorage.getItem(this.storageKey); if (stored) { return Context7Manager.deserialize(stored); } return null; } catch (error) { console.error('Failed to load context:', error); return null; } }
saveContext() { try { localStorage.setItem(this.storageKey, this.context.serialize()); return true; } catch (error) { console.error('Failed to save context:', error); return false; } }
getContext(): Context7Manager { return this.context; }
// Auto-save context on window unload setupAutoSave() { window.addEventListener('beforeunload', () => { this.saveContext(); }); }}Context Middleware
Section titled “Context Middleware”For server-side applications, implement context middleware:
// Express middleware examplefunction context7Middleware(req, res, next) { // Create or retrieve context if (!req.session.context7) { // Initialize new context req.session.context7 = new Context7Manager({ user: { userId: req.user?.id || 'anonymous', authStatus: req.user ? 'authenticated' : 'anonymous' }, temporal: { sessionStartTime: Date.now() }, environmental: { // Extract from request headers operatingSystem: parseUserAgent(req.headers['user-agent']), network: { type: req.headers['connection-type'] || 'unknown' } } }); }
// Update context with current request info req.session.context7.updateTemporalContext({ timestamp: Date.now() });
req.session.context7.updateSpatialContext({ virtualLocation: { currentRoute: req.path, previousRoute: req.session.lastPath || '', // Add to navigation stack if it's a new path navigationStack: req.session.lastPath !== req.path ? [...req.session.context7.getSpatialContext().virtualLocation.navigationStack, req.path] : req.session.context7.getSpatialContext().virtualLocation.navigationStack } });
// Record the action req.session.context7.recordUserAction(`request_${req.method.toLowerCase()}`, { path: req.path, query: req.query });
// Save last path for next request req.session.lastPath = req.path;
// Make context available to route handlers req.context7 = req.session.context7;
next();}Usage Examples
Section titled “Usage Examples”Example 1: Context-Aware Debugging
Section titled “Example 1: Context-Aware Debugging”// Example of context-aware debuggingclass ContextAwareDebugger { private context: Context7Manager;
constructor(initialContext?: Context7Manager) { this.context = initialContext || new Context7Manager(); }
logError(error: Error, source: string) { // Capture full context at the time of error const errorContext = { error: { message: error.message, stack: error.stack, name: error.name, source }, context: this.context.getFullContext() };
// Log enhanced error console.error('Context-aware error:', JSON.stringify(errorContext, null, 2));
// Optionally send to error tracking service // this.sendToErrorTrackingService(errorContext);
return errorContext; }
async diagnose(error: Error) { // Use context to enhance error diagnosis const userContext = this.context.getUserContext(); const stateContext = this.context.getStateContext(); const envContext = this.context.getEnvironmentalContext();
console.log('Diagnostic information:'); console.log('- User:', userContext.userId); console.log('- Auth status:', userContext.authStatus); console.log('- Application state:', Object.keys(stateContext.applicationState)); console.log('- Environment:', envContext.operatingSystem); console.log('- Recent actions:', this.context.getTemporalContext().actionHistory.slice(-5));
// Suggest potential fixes based on context return this.suggestFixes(error); }
private suggestFixes(error: Error) { // Implementation to suggest fixes based on error and context // ... return []; }}Example 2: Personalized User Experience
Section titled “Example 2: Personalized User Experience”// Example of context-aware UI personalizationclass ContextAwareUI { private context: Context7Manager;
constructor(initialContext?: Context7Manager) { this.context = initialContext || new Context7Manager(); }
renderDashboard() { const userContext = this.context.getUserContext(); const taskContext = this.context.getTaskContext(); const temporalContext = this.context.getTemporalContext();
// Personalize dashboard based on context return { layout: userContext.preferences.dashboardLayout || 'default', theme: userContext.profile.theme, widgets: this.getPersonalizedWidgets(), recentItems: this.getRecentItems(temporalContext), suggestions: this.getSuggestions(userContext, taskContext), notifications: this.getRelevantNotifications() }; }
private getPersonalizedWidgets() { const userContext = this.context.getUserContext();
// Base widgets everyone gets const widgets = ['messages', 'calendar'];
// Add expertise-based widgets if (userContext.profile.expertise === 'advanced') { widgets.push('terminal', 'metrics', 'api-status'); }
// Add role-based widgets if (userContext.permissions.includes('admin')) { widgets.push('admin-panel', 'user-management'); }
// Add widgets based on usage patterns const actionFrequency = this.context.getTemporalContext().frequencyMap; if ((actionFrequency['view_documentation'] || 0) > 5) { widgets.push('documentation-shortcuts'); }
return widgets; }
private getRecentItems(temporalContext: TemporalContext) { // Extract recently accessed items from action history // ... return []; }
private getSuggestions(userContext: UserContext, taskContext: TaskContext) { // Generate contextually relevant suggestions // ... return []; }
private getRelevantNotifications() { // Filter notifications based on context // ... return []; }}Example 3: Multi-Session State Management
Section titled “Example 3: Multi-Session State Management”// Example of context persistence across sessionsclass PersistentContextManager { private context: Context7Manager; private storageKey: string;
constructor(userId: string) { this.storageKey = `user-context-${userId}`; this.context = this.loadContext() || new Context7Manager();
// Set up auto-save this.setupAutoSave(); }
private loadContext(): Context7Manager | null { try { // Try to load from localStorage if (typeof localStorage !== 'undefined') { const saved = localStorage.getItem(this.storageKey); if (saved) { return Context7Manager.deserialize(saved); } }
// Try to load from server if localStorage failed // ...
return null; } catch (error) { console.error('Failed to load context:', error); return null; } }
private setupAutoSave() { // Save context every 30 seconds setInterval(() => { this.saveContext(); }, 30000);
// Save on page unload if (typeof window !== 'undefined') { window.addEventListener('beforeunload', () => { this.saveContext(); }); } }
saveContext() { try { // Update timestamp before saving this.context.updateTemporalContext({ timestamp: Date.now() });
// Save to localStorage if (typeof localStorage !== 'undefined') { localStorage.setItem(this.storageKey, this.context.serialize()); }
// Optionally save to server // this.saveToServer();
return true; } catch (error) { console.error('Failed to save context:', error); return false; } }
getContext() { return this.context; }
clearContext() { if (typeof localStorage !== 'undefined') { localStorage.removeItem(this.storageKey); } this.context = new Context7Manager(); return true; }}Example 4: Collaborative Environment
Section titled “Example 4: Collaborative Environment”// Example of shared context in a collaborative environmentclass CollaborativeContext { private localContext: Context7Manager; private sharedContext: Context7Manager; private collaborationId: string; private socket: WebSocket;
constructor(collaborationId: string, initialLocalContext?: Context7Manager) { this.collaborationId = collaborationId; this.localContext = initialLocalContext || new Context7Manager(); this.sharedContext = new Context7Manager();
// Connect to collaboration server this.connectToCollaborationServer(); }
private connectToCollaborationServer() { // Connect to WebSocket server for real-time context sharing this.socket = new WebSocket(`wss://collaboration.example.com/${this.collaborationId}`);
this.socket.onopen = () => { // Request current shared context this.socket.send(JSON.stringify({ type: 'get_shared_context' })); };
this.socket.onmessage = (event) => { const message = JSON.parse(event.data);
if (message.type === 'shared_context_update') { // Update our copy of the shared context this.sharedContext = Context7Manager.deserialize(message.context); } }; }
// Update the shared context updateSharedContext(update: Partial<Context7Manager>) { // Apply update to local copy of shared context // ...
// Send update to server this.socket.send(JSON.stringify({ type: 'update_shared_context', update: JSON.stringify(update) })); }
// Get combined context (local + shared) getCombinedContext() { // Merge local and shared context // Priority to local context for conflicts // ...
return { local: this.localContext, shared: this.sharedContext, // combined: mergedContext }; }
// Share an action from local to shared context shareAction(action: string, metadata?: any) { const userId = this.localContext.getUserContext().userId;
this.updateSharedContext({ temporal: { actionHistory: [ ...this.sharedContext.getTemporalContext().actionHistory, { action, userId, timestamp: Date.now(), metadata } ] } }); }}Example 5: AI-Assisted Development
Section titled “Example 5: AI-Assisted Development”// Example of AI-assisted development with context retentionclass ContextAwareAIAssistant { private context: Context7Manager; private llmClient: any; // Simplified for example
constructor(llmClient: any, initialContext?: Context7Manager) { this.context = initialContext || new Context7Manager(); this.llmClient = llmClient; }
async generateCodeSuggestion(codeContext: string) { // Get relevant context for the AI const userContext = this.context.getUserContext(); const taskContext = this.context.getTaskContext(); const semanticContext = this.context.getSemanticContext();
// Compose prompt with rich context const prompt = ` You are assisting a ${userContext.profile.expertise} level developer.
Current task: ${taskContext.currentTask?.description}
Recent actions: ${this.context.getTemporalContext().actionHistory.slice(-5).map(a => `- ${a.action} at ${new Date(a.timestamp).toISOString()}` ).join('\n')}
Related entities: ${Array.from(semanticContext.entities.entries()) .filter(([_, entity]) => entity.type === 'code') .slice(0, 3) .map(([key, entity]) => `- ${key}: ${JSON.stringify(entity.attributes)}`) .join('\n') }
Code context: \`\`\` ${codeContext} \`\`\`
Generate a code suggestion that would help complete the current task. `;
// Call LLM with context-enhanced prompt const suggestion = await this.llmClient.complete(prompt);
// Update context with this interaction this.context.recordUserAction('requested_code_suggestion'); this.context.updateSemanticContext({ entities: new Map([ ...Array.from(semanticContext.entities.entries()), [`suggestion-${Date.now()}`, { type: 'suggestion', attributes: { prompt: prompt.substring(0, 100) + '...', response: suggestion.substring(0, 100) + '...', timestamp: Date.now() } }] ]) });
return suggestion; }
async explainCode(code: string) { // Similar implementation with context-aware prompting // ... return ""; }
async suggestRefactoring(code: string) { // Similar implementation with context-aware prompting // ... return ""; }}Conclusion
Section titled “Conclusion”The Context7 MCP component provides a comprehensive framework for managing context across seven dimensions, enabling richer, more intelligent software systems. By implementing Context7 in your applications, you can enhance user experiences, improve AI interactions, support collaborative environments, and build more adaptive systems.
Context7 works seamlessly with other MCP components like Sequential Thinking, Playwright, and Serena to provide a complete solution for complex software development challenges. The multi-dimensional approach ensures that no aspect of context is overlooked, leading to more robust and sophisticated applications.