# Directory Structure ``` ├── .env ├── .python-version ├── main.py ├── node │ └── mem0 │ ├── .gitignore │ ├── package.json │ ├── pnpm-lock.yaml │ ├── pnpm-workspace.yaml │ ├── README.md │ ├── smithery.yaml │ ├── src │ │ └── index.ts │ ├── tsconfig.json │ └── tsup.config.ts ├── pyproject.toml ├── README.md └── uv.lock ``` # Files -------------------------------------------------------------------------------- /.python-version: -------------------------------------------------------------------------------- ``` 1 | 3.12 2 | ``` -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- ``` 1 | MEM0_API_KEY=<your-api-key> ``` -------------------------------------------------------------------------------- /node/mem0/.gitignore: -------------------------------------------------------------------------------- ``` 1 | **/.env 2 | **/.env.local 3 | **/.env.development.local 4 | **/.env.test.local 5 | **/.env.production.local 6 | **/.env.development 7 | **/.env.test 8 | 9 | node_modules 10 | build 11 | dist 12 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- ```markdown 1 | # MCP Server with Mem0 for Managing Coding Preferences 2 | 3 | This demonstrates a structured approach for using an [MCP](https://modelcontextprotocol.io/introduction) server with [mem0](https://mem0.ai) to manage coding preferences efficiently. The server can be used with Cursor and provides essential tools for storing, retrieving, and searching coding preferences. 4 | 5 | ## Installation 6 | 7 | 1. Clone this repository 8 | 2. Initialize the `uv` environment: 9 | 10 | ```bash 11 | uv venv 12 | ``` 13 | 14 | 3. Activate the virtual environment: 15 | 16 | ```bash 17 | source .venv/bin/activate 18 | ``` 19 | 20 | 4. Install the dependencies using `uv`: 21 | 22 | ```bash 23 | # Install in editable mode from pyproject.toml 24 | uv pip install -e . 25 | ``` 26 | 27 | 5. Update `.env` file in the root directory with your mem0 API key: 28 | 29 | ```bash 30 | MEM0_API_KEY=your_api_key_here 31 | ``` 32 | 33 | ## Usage 34 | 35 | 1. Start the MCP server: 36 | 37 | ```bash 38 | uv run main.py 39 | ``` 40 | 41 | 2. In Cursor, connect to the SSE endpoint, follow this [doc](https://docs.cursor.com/context/model-context-protocol) for reference: 42 | 43 | ``` 44 | http://0.0.0.0:8080/sse 45 | ``` 46 | 47 | 3. Open the Composer in Cursor and switch to `Agent` mode. 48 | 49 | ## Demo with Cursor 50 | 51 | https://github.com/user-attachments/assets/56670550-fb11-4850-9905-692d3496231c 52 | 53 | ## Features 54 | 55 | The server provides three main tools for managing code preferences: 56 | 57 | 1. `add_coding_preference`: Store code snippets, implementation details, and coding patterns with comprehensive context including: 58 | - Complete code with dependencies 59 | - Language/framework versions 60 | - Setup instructions 61 | - Documentation and comments 62 | - Example usage 63 | - Best practices 64 | 65 | 2. `get_all_coding_preferences`: Retrieve all stored coding preferences to analyze patterns, review implementations, and ensure no relevant information is missed. 66 | 67 | 3. `search_coding_preferences`: Semantically search through stored coding preferences to find relevant: 68 | - Code implementations 69 | - Programming solutions 70 | - Best practices 71 | - Setup guides 72 | - Technical documentation 73 | 74 | ## Why? 75 | 76 | This implementation allows for a persistent coding preferences system that can be accessed via MCP. The SSE-based server can run as a process that agents connect to, use, and disconnect from whenever needed. This pattern fits well with "cloud-native" use cases where the server and clients can be decoupled processes on different nodes. 77 | 78 | ### Server 79 | 80 | By default, the server runs on 0.0.0.0:8080 but is configurable with command line arguments like: 81 | 82 | ``` 83 | uv run main.py --host <your host> --port <your port> 84 | ``` 85 | 86 | The server exposes an SSE endpoint at `/sse` that MCP clients can connect to for accessing the coding preferences management tools. 87 | 88 | ``` -------------------------------------------------------------------------------- /node/mem0/README.md: -------------------------------------------------------------------------------- ```markdown 1 | # Mem0 Memory MCP Server 2 | 3 | A Model Context Protocol (MCP) server that provides memory storage and retrieval capabilities using [Mem0](https://github.com/mem0ai/mem0). This tool allows you to store and search through memories, making it useful for maintaining context and making informed decisions based on past interactions. 4 | 5 | ## Features 6 | 7 | - Store memories with user-specific context 8 | - Search through stored memories with relevance scoring 9 | - Simple and intuitive API 10 | - Built on the Model Context Protocol 11 | - Automatic error handling 12 | - Support for multiple user contexts 13 | 14 | ## Usage 15 | 16 | This server now supports StreamableHTTP via Smithery CLI while retaining optional STDIO compatibility. 17 | 18 | ### StreamableHTTP (recommended) 19 | 20 | - Development (opens Smithery Playground and exposes HTTP transport): 21 | 22 | ```bash 23 | npm run dev 24 | ``` 25 | 26 | - Build for HTTP/StreamableHTTP (Smithery): 27 | 28 | ```bash 29 | npm run build 30 | ``` 31 | 32 | - Start the HTTP server locally (Smithery-built entrypoint): 33 | 34 | ```bash 35 | npm run start:http 36 | ``` 37 | 38 | You can configure the server using Smithery’s generated form in the playground or by setting environment variables (e.g., `MEM0_API_KEY`). 39 | 40 | ### STDIO (backward compatible) 41 | 42 | Run the server over STDIO (useful for local clients that only support STDIO): 43 | 44 | ```bash 45 | env MEM0_API_KEY=your-api-key-here npm run build:stdio && npm run start:stdio 46 | ``` 47 | 48 | ## Configuration for AI Tools 49 | 50 | ### Running on Cursor (STDIO) 51 | 52 | #### Configuring Cursor 🖥️ 53 | 54 | To configure Mem0 MCP in Cursor: 55 | 56 | 1. Open Cursor Settings 57 | 2. Go to Features > MCP Servers 58 | 3. Click "+ Add New MCP Server" 59 | 4. Enter the following: 60 | - Name: "mem0-mcp" (or your preferred name) 61 | - Type: "command" 62 | - Command: `env MEM0_API_KEY=your-api-key-here npx -y @mem0/mcp` (or use `start:stdio` from this repo) 63 | 64 | To configure Mem0 MCP using JSON configuration: 65 | 66 | ```json 67 | { 68 | "mcpServers": { 69 | "mem0-mcp": { 70 | "command": "npx", 71 | "args": ["-y", "@mem0/mcp"], 72 | "env": { 73 | "MEM0_API_KEY": "YOUR-API-KEY-HERE" 74 | } 75 | } 76 | } 77 | } 78 | ``` 79 | 80 | ### Running on VS Code (STDIO) 81 | 82 | Add the following JSON block to your User Settings (JSON) file in VS Code: 83 | 84 | ```json 85 | { 86 | "mcp": { 87 | "inputs": [ 88 | { 89 | "type": "promptString", 90 | "id": "apiKey", 91 | "description": "Mem0 API Key", 92 | "password": true 93 | } 94 | ], 95 | "servers": { 96 | "mem0-memory": { 97 | "command": "npx", 98 | "args": ["-y", "@mem0/mcp"], 99 | "env": { 100 | "MEM0_API_KEY": "${input:apiKey}" 101 | } 102 | } 103 | } 104 | } 105 | } 106 | ``` 107 | 108 | ## Available Tools 109 | 110 | ### 1. Add Memory Tool (add-memory) 111 | 112 | Store new memories with user-specific context. 113 | 114 | ```json 115 | { 116 | "name": "add-memory", 117 | "arguments": { 118 | "content": "User prefers dark mode interface", 119 | "userId": "user123" 120 | } 121 | } 122 | ``` 123 | 124 | ### 2. Search Memories Tool (search-memories) 125 | 126 | Search through stored memories to retrieve relevant information. 127 | 128 | ```json 129 | { 130 | "name": "search-memories", 131 | "arguments": { 132 | "query": "What are the user's interface preferences?", 133 | "userId": "user123" 134 | } 135 | } 136 | ``` 137 | 138 | ## Response Format 139 | 140 | ### Add Memory Response 141 | 142 | ```json 143 | { 144 | "content": [ 145 | { 146 | "type": "text", 147 | "text": "Memory added successfully" 148 | } 149 | ], 150 | "isError": false 151 | } 152 | ``` 153 | 154 | ### Search Memory Response 155 | 156 | ```json 157 | { 158 | "content": [ 159 | { 160 | "type": "text", 161 | "text": "Memory: User prefers dark mode interface\nRelevance: 0.95\n---\nMemory: User mentioned liking minimal UI\nRelevance: 0.82\n---" 162 | } 163 | ], 164 | "isError": false 165 | } 166 | ``` 167 | 168 | ## Configuration 169 | 170 | ### Environment Variables 171 | 172 | - `MEM0_API_KEY`: Your Mem0 API key (required) 173 | - Required for operation 174 | - Can be obtained from [Mem0 Dashboard](https://app.mem0.ai/dashboard/api-keys) 175 | 176 | ## Development 177 | 178 | ### Prerequisites 179 | 180 | - Node.js >= 18 181 | - A Mem0 API key 182 | 183 | ### Setup 184 | 185 | 1. Install dependencies: 186 | 187 | ```bash 188 | npm install 189 | ``` 190 | 191 | 2. Optionally create a `.env` file in the project directory and add your Mem0 API key: 192 | 193 | ```bash 194 | MEM0_API_KEY=your-api-key-here 195 | DEFAULT_USER_ID=mem0-mcp-user 196 | ``` 197 | 198 | ### HTTP/StreamableHTTP Dev 199 | 200 | ```bash 201 | npm run dev 202 | ``` 203 | 204 | ### STDIO Dev 205 | 206 | ```bash 207 | npm run build:stdio 208 | npm run start:stdio 209 | ``` 210 | 211 | ## Error Handling 212 | 213 | The server includes error handling for: 214 | 215 | - API connection issues 216 | - Invalid memory operations 217 | - Search errors 218 | - Authentication failures 219 | 220 | Example error response: 221 | 222 | ```json 223 | { 224 | "content": [ 225 | { 226 | "type": "text", 227 | "text": "Error: Failed to search memories: Invalid API key" 228 | } 229 | ], 230 | "isError": true 231 | } 232 | ``` 233 | 234 | ## Contributing 235 | 236 | Contributions are welcome! Please feel free to submit a Pull Request. 237 | 238 | ## License 239 | 240 | MIT 241 | ``` -------------------------------------------------------------------------------- /node/mem0/smithery.yaml: -------------------------------------------------------------------------------- ```yaml 1 | runtime: typescript 2 | entry: src/index.ts 3 | ``` -------------------------------------------------------------------------------- /node/mem0/pnpm-workspace.yaml: -------------------------------------------------------------------------------- ```yaml 1 | onlyBuiltDependencies: 2 | - esbuild 3 | - sqlite3 4 | ``` -------------------------------------------------------------------------------- /node/mem0/tsup.config.ts: -------------------------------------------------------------------------------- ```typescript 1 | import { defineConfig } from 'tsup' 2 | 3 | export default defineConfig([ 4 | { 5 | dts: true, 6 | entry: ['src/index.ts'], 7 | format: ['cjs', 'esm'], 8 | sourcemap: true, 9 | }, 10 | ]) ``` -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- ```toml 1 | [project] 2 | name = "mem0-mcp" 3 | version = "0.1.0" 4 | description = "Add your description here" 5 | readme = "README.md" 6 | requires-python = ">=3.12" 7 | dependencies = [ 8 | "httpx>=0.28.1", 9 | "mcp[cli]>=1.3.0", 10 | "mem0ai>=0.1.55", 11 | ] 12 | ``` -------------------------------------------------------------------------------- /node/mem0/tsconfig.json: -------------------------------------------------------------------------------- ```json 1 | { 2 | "compilerOptions": { 3 | "target": "ES2022", 4 | "module": "Node16", 5 | "moduleResolution": "Node16", 6 | "outDir": "./build", 7 | "rootDir": "./src", 8 | "strict": true, 9 | "esModuleInterop": true, 10 | "skipLibCheck": true, 11 | "forceConsistentCasingInFileNames": true 12 | }, 13 | "include": ["src/**/*"], 14 | "exclude": ["node_modules"] 15 | } ``` -------------------------------------------------------------------------------- /node/mem0/package.json: -------------------------------------------------------------------------------- ```json 1 | { 2 | "name": "@mem0/mcp-server", 3 | "version": "0.0.1", 4 | "main": "dist/index.js", 5 | "type": "module", 6 | "module": "src/index.ts", 7 | "bin": { 8 | "mem0-mcp": "dist/index.js" 9 | }, 10 | "scripts": { 11 | "dev": "npx @smithery/cli dev", 12 | "build": "npm run build:http", 13 | "build:stdio": "tsup", 14 | "build:http": "npx @smithery/cli build", 15 | "start": "npm run start:http", 16 | "start:http": "node .smithery/index.cjs", 17 | "start:stdio": "node dist/index.js", 18 | "clean": "rm -rf dist", 19 | "prepublishOnly": "npm run build:stdio" 20 | }, 21 | "files": [ 22 | "dist" 23 | ], 24 | "publishConfig": { 25 | "access": "public" 26 | }, 27 | "keywords": [ 28 | "mcp", 29 | "mem0", 30 | "memory", 31 | "ai", 32 | "agents" 33 | ], 34 | "author": "", 35 | "license": "ISC", 36 | "description": "", 37 | "dependencies": { 38 | "@modelcontextprotocol/sdk": "^1.17.4", 39 | "dotenv": "^16.5.0", 40 | "mem0ai": "^2.1.38", 41 | "tsup": "^8.4.0", 42 | "zod": "^3.24.2" 43 | }, 44 | "devDependencies": { 45 | "@types/node": "^22.13.13", 46 | "typescript": "^5.8.2", 47 | "tsx": "^4.19.0" 48 | }, 49 | "packageManager": "[email protected]+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977" 50 | } 51 | ``` -------------------------------------------------------------------------------- /node/mem0/src/index.ts: -------------------------------------------------------------------------------- ```typescript 1 | import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; 2 | import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; 3 | import { MemoryClient, Message } from 'mem0ai'; 4 | import dotenv from 'dotenv'; 5 | import { z } from 'zod'; 6 | 7 | dotenv.config(); 8 | 9 | // Session configuration schema (used by Smithery CLI for HTTP/StreamableHTTP) 10 | export const configSchema = z.object({ 11 | mem0ApiKey: z 12 | .string() 13 | .describe('Mem0 API key. Defaults to MEM0_API_KEY env var if not provided.'), 14 | defaultUserId: z 15 | .string() 16 | .optional() 17 | .default('mem0-mcp-user') 18 | .describe("Default user ID when not provided in tool input"), 19 | }); 20 | 21 | // Factory to create the MCP server. Smithery CLI will call this for HTTP transport. 22 | export default function createServer({ 23 | config, 24 | }: { 25 | config: z.infer<typeof configSchema>; 26 | }) { 27 | const apiKey = config.mem0ApiKey || process?.env?.MEM0_API_KEY || ''; 28 | const defaultUserId = config.defaultUserId || 'mem0-mcp-user'; 29 | 30 | const memoryClient = new MemoryClient({ apiKey }); 31 | 32 | const server = new McpServer({ 33 | name: 'mem0-mcp', 34 | version: '0.0.1', 35 | }); 36 | 37 | // add-memory tool 38 | server.tool( 39 | 'add-memory', 40 | 'Add a new memory about the user. Call this whenever the user shares preferences, facts about themselves, or explicitly asks you to remember something.', 41 | { 42 | content: z.string().describe('The content to store in memory'), 43 | userId: z 44 | .string() 45 | .optional() 46 | .describe("User ID for memory storage. If omitted, uses config.defaultUserId."), 47 | }, 48 | async ({ content, userId }) => { 49 | const resolvedUserId = userId || defaultUserId; 50 | try { 51 | const messages: Message[] = [ 52 | { role: 'user', content: content } 53 | ]; 54 | memoryClient.add(messages, { user_id: resolvedUserId, async_mode: true, version: "v2", output_format: "v1.1" }); 55 | return { 56 | content: [ 57 | { 58 | type: 'text', 59 | text: 'Memory added successfully', 60 | }, 61 | ], 62 | }; 63 | } catch (error) { 64 | return { 65 | content: [ 66 | { 67 | type: 'text', 68 | text: 69 | 'Error adding memory: ' + 70 | (error instanceof Error ? error.message : String(error)), 71 | }, 72 | ], 73 | isError: true, 74 | } as const; 75 | } 76 | } 77 | ); 78 | 79 | // search-memories tool 80 | server.tool( 81 | 'search-memories', 82 | 'Search through stored memories. Call this whenever you need to recall prior information relevant to the user query.', 83 | { 84 | query: z 85 | .string() 86 | .describe( 87 | "The search query, typically derived from the user's current question." 88 | ), 89 | userId: z 90 | .string() 91 | .optional() 92 | .describe("User ID for memory storage. If omitted, uses config.defaultUserId."), 93 | }, 94 | async ({ query, userId }) => { 95 | const resolvedUserId = userId || defaultUserId; 96 | try { 97 | const results: any[] = await memoryClient.search(query, { 98 | user_id: resolvedUserId, 99 | }); 100 | const formattedResults = (results || []) 101 | .map((result: any) => `Memory: ${result.memory}\nRelevance: ${result.score}\n---`) 102 | .join('\n'); 103 | 104 | return { 105 | content: [ 106 | { 107 | type: 'text', 108 | text: formattedResults || 'No memories found', 109 | }, 110 | ], 111 | }; 112 | } catch (error) { 113 | return { 114 | content: [ 115 | { 116 | type: 'text', 117 | text: 118 | 'Error searching memories: ' + 119 | (error instanceof Error ? error.message : String(error)), 120 | }, 121 | ], 122 | isError: true, 123 | } as const; 124 | } 125 | } 126 | ); 127 | 128 | return server.server; 129 | } 130 | 131 | // Optional: keep STDIO compatibility for local usage 132 | async function main() { 133 | try { 134 | console.error('Initializing Mem0 Memory MCP Server (stdio mode)...'); 135 | 136 | const server = createServer({ 137 | config: { 138 | mem0ApiKey: process?.env?.MEM0_API_KEY, 139 | defaultUserId: process?.env?.DEFAULT_USER_ID || 'mem0-mcp-user', 140 | }, 141 | }); 142 | 143 | const transport = new StdioServerTransport(); 144 | await server.connect(transport); 145 | 146 | console.error('Memory MCP Server running on stdio'); 147 | } catch (error) { 148 | console.error('Fatal error running server:', error); 149 | process.exit(1); 150 | } 151 | } 152 | 153 | // Execute main when launched directly via node dist/index.js 154 | if (process.argv[1] && process.argv[1].includes('index.js')) { 155 | // best-effort detection; avoids executing during smithery-cli runtime 156 | main().catch((error) => { 157 | console.error('Fatal error in main():', error); 158 | process.exit(1); 159 | }); 160 | } 161 | ``` -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- ```python 1 | from mcp.server.fastmcp import FastMCP 2 | from starlette.applications import Starlette 3 | from mcp.server.sse import SseServerTransport 4 | from starlette.requests import Request 5 | from starlette.routing import Mount, Route 6 | from mcp.server import Server 7 | import uvicorn 8 | from mem0 import MemoryClient 9 | from dotenv import load_dotenv 10 | import json 11 | 12 | load_dotenv() 13 | 14 | # Initialize FastMCP server for mem0 tools 15 | mcp = FastMCP("mem0-mcp") 16 | 17 | # Initialize mem0 client and set default user 18 | mem0_client = MemoryClient() 19 | DEFAULT_USER_ID = "cursor_mcp" 20 | CUSTOM_INSTRUCTIONS = """ 21 | Extract the Following Information: 22 | 23 | - Code Snippets: Save the actual code for future reference. 24 | - Explanation: Document a clear description of what the code does and how it works. 25 | - Related Technical Details: Include information about the programming language, dependencies, and system specifications. 26 | - Key Features: Highlight the main functionalities and important aspects of the snippet. 27 | """ 28 | mem0_client.update_project(custom_instructions=CUSTOM_INSTRUCTIONS) 29 | 30 | @mcp.tool( 31 | description="""Add a new coding preference to mem0. This tool stores code snippets, implementation details, 32 | and coding patterns for future reference. Store every code snippet. When storing code, you should include: 33 | - Complete code with all necessary imports and dependencies 34 | - Language/framework version information (e.g., "Python 3.9", "React 18") 35 | - Full implementation context and any required setup/configuration 36 | - Detailed comments explaining the logic, especially for complex sections 37 | - Example usage or test cases demonstrating the code 38 | - Any known limitations, edge cases, or performance considerations 39 | - Related patterns or alternative approaches 40 | - Links to relevant documentation or resources 41 | - Environment setup requirements (if applicable) 42 | - Error handling and debugging tips 43 | The preference will be indexed for semantic search and can be retrieved later using natural language queries.""" 44 | ) 45 | async def add_coding_preference(text: str) -> str: 46 | """Add a new coding preference to mem0. 47 | 48 | This tool is designed to store code snippets, implementation patterns, and programming knowledge. 49 | When storing code, it's recommended to include: 50 | - Complete code with imports and dependencies 51 | - Language/framework information 52 | - Setup instructions if needed 53 | - Documentation and comments 54 | - Example usage 55 | 56 | Args: 57 | text: The content to store in memory, including code, documentation, and context 58 | """ 59 | try: 60 | messages = [{"role": "user", "content": text}] 61 | mem0_client.add(messages, user_id=DEFAULT_USER_ID, output_format="v1.1") 62 | return f"Successfully added preference: {text}" 63 | except Exception as e: 64 | return f"Error adding preference: {str(e)}" 65 | 66 | @mcp.tool( 67 | description="""Retrieve all stored coding preferences for the default user. Call this tool when you need 68 | complete context of all previously stored preferences. This is useful when: 69 | - You need to analyze all available code patterns 70 | - You want to check all stored implementation examples 71 | - You need to review the full history of stored solutions 72 | - You want to ensure no relevant information is missed 73 | Returns a comprehensive list of: 74 | - Code snippets and implementation patterns 75 | - Programming knowledge and best practices 76 | - Technical documentation and examples 77 | - Setup and configuration guides 78 | Results are returned in JSON format with metadata.""" 79 | ) 80 | async def get_all_coding_preferences() -> str: 81 | """Get all coding preferences for the default user. 82 | 83 | Returns a JSON formatted list of all stored preferences, including: 84 | - Code implementations and patterns 85 | - Technical documentation 86 | - Programming best practices 87 | - Setup guides and examples 88 | Each preference includes metadata about when it was created and its content type. 89 | """ 90 | try: 91 | memories = mem0_client.get_all(user_id=DEFAULT_USER_ID, page=1, page_size=50) 92 | flattened_memories = [memory["memory"] for memory in memories["results"]] 93 | return json.dumps(flattened_memories, indent=2) 94 | except Exception as e: 95 | return f"Error getting preferences: {str(e)}" 96 | 97 | @mcp.tool( 98 | description="""Search through stored coding preferences using semantic search. This tool should be called 99 | for EVERY user query to find relevant code and implementation details. It helps find: 100 | - Specific code implementations or patterns 101 | - Solutions to programming problems 102 | - Best practices and coding standards 103 | - Setup and configuration guides 104 | - Technical documentation and examples 105 | The search uses natural language understanding to find relevant matches, so you can 106 | describe what you're looking for in plain English. Always search the preferences before 107 | providing answers to ensure you leverage existing knowledge.""" 108 | ) 109 | async def search_coding_preferences(query: str) -> str: 110 | """Search coding preferences using semantic search. 111 | 112 | The search is powered by natural language understanding, allowing you to find: 113 | - Code implementations and patterns 114 | - Programming solutions and techniques 115 | - Technical documentation and guides 116 | - Best practices and standards 117 | Results are ranked by relevance to your query. 118 | 119 | Args: 120 | query: Search query string describing what you're looking for. Can be natural language 121 | or specific technical terms. 122 | """ 123 | try: 124 | memories = mem0_client.search(query, user_id=DEFAULT_USER_ID, output_format="v1.1") 125 | flattened_memories = [memory["memory"] for memory in memories["results"]] 126 | return json.dumps(flattened_memories, indent=2) 127 | except Exception as e: 128 | return f"Error searching preferences: {str(e)}" 129 | 130 | def create_starlette_app(mcp_server: Server, *, debug: bool = False) -> Starlette: 131 | """Create a Starlette application that can server the provied mcp server with SSE.""" 132 | sse = SseServerTransport("/messages/") 133 | 134 | async def handle_sse(request: Request) -> None: 135 | async with sse.connect_sse( 136 | request.scope, 137 | request.receive, 138 | request._send, # noqa: SLF001 139 | ) as (read_stream, write_stream): 140 | await mcp_server.run( 141 | read_stream, 142 | write_stream, 143 | mcp_server.create_initialization_options(), 144 | ) 145 | 146 | return Starlette( 147 | debug=debug, 148 | routes=[ 149 | Route("/sse", endpoint=handle_sse), 150 | Mount("/messages/", app=sse.handle_post_message), 151 | ], 152 | ) 153 | 154 | 155 | if __name__ == "__main__": 156 | mcp_server = mcp._mcp_server 157 | 158 | import argparse 159 | 160 | parser = argparse.ArgumentParser(description='Run MCP SSE-based server') 161 | parser.add_argument('--host', default='0.0.0.0', help='Host to bind to') 162 | parser.add_argument('--port', type=int, default=8080, help='Port to listen on') 163 | args = parser.parse_args() 164 | 165 | # Bind SSE request handling to MCP server 166 | starlette_app = create_starlette_app(mcp_server, debug=True) 167 | 168 | uvicorn.run(starlette_app, host=args.host, port=args.port) 169 | ```