From 541aa19649850fb99486b9072872661e1c31cf5f Mon Sep 17 00:00:00 2001 From: GongRzhe <1339673722@qq.com> Date: Tue, 24 Dec 2024 12:20:29 +0800 Subject: [PATCH 1/2] Add Redis Server # Redis A Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools. --- src/redis/Dockerfile | 23 ++++ src/redis/README.md | 80 ++++++++++++++ src/redis/package.json | 28 +++++ src/redis/src/index.ts | 236 ++++++++++++++++++++++++++++++++++++++++ src/redis/tsconfig.json | 16 +++ 5 files changed, 383 insertions(+) create mode 100644 src/redis/Dockerfile create mode 100644 src/redis/README.md create mode 100644 src/redis/package.json create mode 100644 src/redis/src/index.ts create mode 100644 src/redis/tsconfig.json diff --git a/src/redis/Dockerfile b/src/redis/Dockerfile new file mode 100644 index 00000000..24a2bb87 --- /dev/null +++ b/src/redis/Dockerfile @@ -0,0 +1,23 @@ +FROM node:22.12-alpine as builder + +COPY src/redis /app + +WORKDIR /app + +RUN --mount=type=cache,target=/root/.npm npm install + +RUN npm run build + +FROM node:22-alpine AS release + +COPY --from=builder /app/build /app/build +COPY --from=builder /app/package.json /app/package.json +COPY --from=builder /app/package-lock.json /app/package-lock.json + +ENV NODE_ENV=production + +WORKDIR /app + +RUN npm ci --ignore-scripts --omit-dev + +ENTRYPOINT ["node", "build/index.js"] \ No newline at end of file diff --git a/src/redis/README.md b/src/redis/README.md new file mode 100644 index 00000000..45123a58 --- /dev/null +++ b/src/redis/README.md @@ -0,0 +1,80 @@ +# Redis + +A Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools. + +## Components + +### Tools + +- **set** + - Set a Redis key-value pair with optional expiration + - Input: + - `key` (string): Redis key + - `value` (string): Value to store + - `expireSeconds` (number, optional): Expiration time in seconds + +- **get** + - Get value by key from Redis + - Input: `key` (string): Redis key to retrieve + +- **delete** + - Delete one or more keys from Redis + - Input: `key` (string | string[]): Key or array of keys to delete + +- **list** + - List Redis keys matching a pattern + - Input: `pattern` (string, optional): Pattern to match keys (default: *) + +## Usage with Claude Desktop + +To use this server with the Claude Desktop app, add the following configuration to the "mcpServers" section of your `claude_desktop_config.json`: + +### Docker + +* when running docker on macos, use host.docker.internal if the server is running on the host network (eg localhost) +* Redis URL can be specified as an argument, defaults to "redis://localhost:6379" + +```json +{ + "mcpServers": { + "redis": { + "command": "docker", + "args": [ + "run", + "-i", + "--rm", + "mcp/redis", + "redis://host.docker.internal:6379"] + } + } +} +``` + +### NPX + +```json +{ + "mcpServers": { + "redis": { + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-redis", + "redis://localhost:6379" + ] + } + } +} +``` + +## Building + +Docker: + +```sh +docker build -t mcp/redis -f src/redis/Dockerfile . +``` + +## License + +This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository. \ No newline at end of file diff --git a/src/redis/package.json b/src/redis/package.json new file mode 100644 index 00000000..fc7f6e64 --- /dev/null +++ b/src/redis/package.json @@ -0,0 +1,28 @@ +{ + "name": "redis", + "version": "1.0.0", + "main": "index.js", + "type": "module", + "bin": { + "redis": "./build/index.js" + }, + "scripts": { + "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"" + }, + "files": [ + "build" + ], + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "devDependencies": { + "@types/node": "^22.10.2", + "typescript": "^5.7.2" + }, + "dependencies": { + "@modelcontextprotocol/sdk": "^0.4.0", + "@types/redis": "^4.0.10", + "redis": "^4.7.0" + } +} diff --git a/src/redis/src/index.ts b/src/redis/src/index.ts new file mode 100644 index 00000000..2d88d981 --- /dev/null +++ b/src/redis/src/index.ts @@ -0,0 +1,236 @@ +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { + CallToolRequestSchema, + ListToolsRequestSchema, +} from "@modelcontextprotocol/sdk/types.js"; +import { z } from "zod"; +import { createClient } from 'redis'; + +// Get Redis URL from command line args or use default +const REDIS_URL = process.argv[2] || "redis://localhost:6379"; +const redisClient = createClient({ + url: REDIS_URL +}); + +// Define Zod schemas for validation +const SetArgumentsSchema = z.object({ + key: z.string(), + value: z.string(), + expireSeconds: z.number().optional(), +}); + +const GetArgumentsSchema = z.object({ + key: z.string(), +}); + +const DeleteArgumentsSchema = z.object({ + key: z.string().or(z.array(z.string())), +}); + +const ListArgumentsSchema = z.object({ + pattern: z.string().default("*"), +}); + +// Create server instance +const server = new Server( + { + name: "redis", + version: "1.0.0" + } +); + +// List available tools +server.setRequestHandler(ListToolsRequestSchema, async () => { + return { + tools: [ + { + name: "set", + description: "Set a Redis key-value pair with optional expiration", + inputSchema: { + type: "object", + properties: { + key: { + type: "string", + description: "Redis key", + }, + value: { + type: "string", + description: "Value to store", + }, + expireSeconds: { + type: "number", + description: "Optional expiration time in seconds", + }, + }, + required: ["key", "value"], + }, + }, + { + name: "get", + description: "Get value by key from Redis", + inputSchema: { + type: "object", + properties: { + key: { + type: "string", + description: "Redis key to retrieve", + }, + }, + required: ["key"], + }, + }, + { + name: "delete", + description: "Delete one or more keys from Redis", + inputSchema: { + type: "object", + properties: { + key: { + oneOf: [ + { type: "string" }, + { type: "array", items: { type: "string" } } + ], + description: "Key or array of keys to delete", + }, + }, + required: ["key"], + }, + }, + { + name: "list", + description: "List Redis keys matching a pattern", + inputSchema: { + type: "object", + properties: { + pattern: { + type: "string", + description: "Pattern to match keys (default: *)", + }, + }, + }, + }, + ], + }; +}); + +// Handle tool execution +server.setRequestHandler(CallToolRequestSchema, async (request) => { + const { name, arguments: args } = request.params; + + try { + if (name === "set") { + const { key, value, expireSeconds } = SetArgumentsSchema.parse(args); + + if (expireSeconds) { + await redisClient.setEx(key, expireSeconds, value); + } else { + await redisClient.set(key, value); + } + + return { + content: [ + { + type: "text", + text: `Successfully set key: ${key}`, + }, + ], + }; + } else if (name === "get") { + const { key } = GetArgumentsSchema.parse(args); + const value = await redisClient.get(key); + + if (value === null) { + return { + content: [ + { + type: "text", + text: `Key not found: ${key}`, + }, + ], + }; + } + + return { + content: [ + { + type: "text", + text: `${value}`, + }, + ], + }; + } else if (name === "delete") { + const { key } = DeleteArgumentsSchema.parse(args); + + if (Array.isArray(key)) { + await redisClient.del(key); + return { + content: [ + { + type: "text", + text: `Successfully deleted ${key.length} keys`, + }, + ], + }; + } else { + await redisClient.del(key); + return { + content: [ + { + type: "text", + text: `Successfully deleted key: ${key}`, + }, + ], + }; + } + } else if (name === "list") { + const { pattern } = ListArgumentsSchema.parse(args); + const keys = await redisClient.keys(pattern); + + return { + content: [ + { + type: "text", + text: keys.length > 0 + ? `Found keys:\n${keys.join('\n')}` + : "No keys found matching pattern", + }, + ], + }; + } else { + throw new Error(`Unknown tool: ${name}`); + } + } catch (error) { + if (error instanceof z.ZodError) { + throw new Error( + `Invalid arguments: ${error.errors + .map((e) => `${e.path.join(".")}: ${e.message}`) + .join(", ")}` + ); + } + throw error; + } +}); + +// Start the server +async function main() { + try { + // Connect to Redis + redisClient.on('error', (err: Error) => console.error('Redis Client Error', err)); + await redisClient.connect(); + console.error(`Connected to Redis successfully at ${REDIS_URL}`); + + const transport = new StdioServerTransport(); + await server.connect(transport); + console.error("Redis MCP Server running on stdio"); + } catch (error) { + console.error("Error during startup:", error); + await redisClient.quit(); + process.exit(1); + } +} + +main().catch((error) => { + console.error("Fatal error in main():", error); + redisClient.quit().finally(() => process.exit(1)); +}); \ No newline at end of file diff --git a/src/redis/tsconfig.json b/src/redis/tsconfig.json new file mode 100644 index 00000000..efcd96e7 --- /dev/null +++ b/src/redis/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "Node16", + "moduleResolution": "Node16", + "outDir": "./build", + "rootDir": "./src", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] + } + \ No newline at end of file From 090b771c29027c0d2f39158fcb102e3ed2e87efe Mon Sep 17 00:00:00 2001 From: GongRzhe <1339673722@qq.com> Date: Wed, 25 Dec 2024 18:46:48 +0800 Subject: [PATCH 2/2] docs: add Redis, Travel Planner, JSON and Calendar MCP servers --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 499e3e41..78c1f0d0 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,10 @@ A growing set of community-developed and maintained servers demonstrates various - **[Atlassian](https://github.com/sooperset/mcp-atlassian)** - Interact with Atlassian Cloud products (Confluence and Jira) including searching/reading Confluence spaces/pages, accessing Jira issues, and project metadata. - **[Google Tasks](https://github.com/zcaceres/gtasks-mcp)** - Google Tasks API Model Context Protocol Server. - **[Fetch](https://github.com/zcaceres/fetch-mcp)** - A server that flexibly fetches HTML, JSON, Markdown, or plaintext - +- **[Redis](https://github.com/GongRzhe/REDIS-MCP-Server)** - Redis database operations and caching microservice server with support for key-value operations, expiration management, and pattern-based key listing. +- **[Travel Planner](https://github.com/GongRzhe/TRAVEL-PLANNER-MCP-Server)** - Travel planning and itinerary management server integrating with Google Maps API for location search, place details, and route calculations. +- **[JSON](https://github.com/GongRzhe/JSON-MCP-Server)** - JSON handling and processing server with advanced query capabilities using JSONPath syntax and support for array, string, numeric, and date operations. +- **[Calendar](https://github.com/GongRzhe/Calendar-MCP-Server)** - Google Calendar integration server enabling AI assistants to manage calendar events through natural language interactions. ## 📚 Resources Additional resources on MCP.