Skip to main content
Glama
jayasimhag1reddy

Context Persistence MCP Server

README.md
# Context Persistence MCP Server

A Model Context Protocol (MCP) server that enables cross-window context sharing in VS Code. This solves the problem of losing conversation context when working across multiple VS Code windows with different repositories.

## Problem Statement

When working with multiple repositories in separate VS Code windows:
- Conversations with Copilot in Window A are not available in Window B
- Analyzing code dependencies across repos requires repeating context
- No shared memory of previous discussions when switching windows

**Example:** You analyze `SparkJobX.java` usage in Repo B, then switch to Repo A to modify the implementation - but Copilot doesn't know about your analysis from Window B.

## Solution

This MCP server provides:
- **Persistent Storage**: All conversations saved to SQLite
- **Cross-Window Access**: Context available across all VS Code windows
- **Smart Matching**: Automatically links related discussions by code entities (classes, files, methods)
- **Hybrid Approach**: Both automatic resources and explicit tools for querying context

## Features

### Tools (Explicit Queries)
- `save_conversation` - Store conversation messages
- `get_related_context` - Find discussions mentioning same classes/files
- `search_conversations` - Full-text search across all workspaces
- `get_workspace_summary` - Overview of all tracked repositories

### Resources (Automatic Context)
- `context://recent` - Last 50 messages across all workspaces
- `context://workspace/{path}` - History for specific repository

## Installation

1. **Install dependencies:**
```bash
npm install
```

2. **Build the server:**
```bash
npm run build
```

3. **Configure in VS Code:**

Add to your Copilot settings (`~/Library/Application Support/Code/User/globalStorage/github.copilot-chat/mcpServers.json` on macOS):

```json
{
  "mcpServers": {
    "context-persistence": {
      "command": "node",
      "args": [
        "/Users/j0k0h1v/Documents/AI/McpServerPoc/dist/index.js"
      ]
    }
  }
}
```

4. **Restart VS Code**

## Usage

### Automatic Context Sharing

When you open any VS Code window, Copilot can access:
- Recent conversations from all windows via `context://recent` resource
- Workspace-specific history via `context://workspace/{path}` resource

### Manual Context Queries

Ask Copilot to use the tools:

```
"Check if we discussed SparkJobX in other repositories"
→ Copilot will call get_related_context with entities: ["SparkJobX"]

"Search our previous conversations about performance optimization"
→ Copilot will call search_conversations with query: "performance optimization"

"What repositories have we analyzed?"
→ Copilot will call get_workspace_summary
```

### Example Workflow

**Window A (ETL-core repo):**
```
You: "Analyze SparkJobX.java performance"
Copilot: [analyzes code] "The job processes 1M records..."
[Context automatically saved]
```

**Window B (Consumer repo):**
```
You: "Why is SparkJobX slow when imported here?"
Copilot: [checks context://recent resource]
         "Based on our previous analysis in ETL-core, SparkJobX processes 1M records..."
```

## Architecture

```
┌─────────────────────────────────┐
│  VS Code Window A (Repo A)      │
│  ├─ Copilot Chat                │
│  └─ MCP Client                  │
└─────────────┬───────────────────┘
              │
              ├─── Tools ──────┐
              │                 │
              ├─── Resources ──┤
              │                 │
        ┌─────▼─────────────────▼───┐
        │  MCP Server                │
        │  ├─ Save conversations     │
        │  ├─ Extract entities       │
        │  └─ Match context          │
        └─────┬──────────────────────┘
              │
        ┌─────▼──────────────────────┐
        │  SQLite Database           │
        │  ~/.context-persistence-mcp│
        │  ├─ messages               │
        │  ├─ code_entities          │
        │  └─ workspace_metadata     │
        └─────┬──────────────────────┘
              │
┌─────────────┴───────────────────┐
│  VS Code Window B (Repo B)      │
│  ├─ Copilot Chat                │
│  └─ MCP Client                  │
└─────────────────────────────────┘
```

## Database Schema

**messages table:**
- `id`: Primary key
- `workspace_path`: Repository path
- `role`: "user" or "assistant"
- `content`: Message text
- `timestamp`: When saved

**code_entities table:**
- `message_id`: Links to message
- `entity_type`: "class", "file", or "method"
- `entity_name`: Name of the entity

**workspace_metadata table:**
- `workspace_path`: Primary key
- `message_count`: Total messages
- `last_active`: Last update timestamp

## How It Works

1. **Entity Extraction**: Automatically detects Java classes, files, and methods in conversations
2. **Smart Storage**: Saves messages with extracted entities for later matching
3. **Context Matching**: When querying, finds messages mentioning same entities
4. **Cross-Window Sync**: All VS Code windows access same SQLite database

## Development

```bash
# Watch mode for development
npm run dev

# Build for production
npm run build

# Run directly
npm start
```

## Database Location

The SQLite database is stored at:
```
~/.context-persistence-mcp/context.db
```

You can inspect it with any SQLite browser or CLI:
```bash
sqlite3 ~/.context-persistence-mcp/context.db
```

## Limitations & Future Enhancements

### Current (MVP):
- Simple keyword matching for entities
- Single-user local storage
- Manual tool invocation by Copilot

### Planned:
- Semantic similarity using embeddings
- Automatic dependency detection from pom.xml/build.gradle
- Repository relationship mapping
- Multi-user support with authentication
- Automatic context injection based on relevance
- Time-based context pruning

## Troubleshooting

**Server not starting?**
- Check the path in `mcpServers.json` is correct
- Verify `dist/index.js` exists after building
- Check VS Code output panel for MCP logs

**No context appearing?**
- Ensure conversations are being saved (ask Copilot to save explicitly first)
- Check database: `sqlite3 ~/.context-persistence-mcp/context.db "SELECT COUNT(*) FROM messages;"`
- Restart VS Code after configuration changes

**Related context not found?**
- Entity extraction is pattern-based - use clear class/file names
- Try explicit search: "search for [keyword] in our conversations"

## Contributing

This is a POC. Feedback and improvements welcome!

## License

MIT