Skip to content

Commit 541aa19

Browse files
committed
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.
1 parent 0e7d79c commit 541aa19

File tree

5 files changed

+383
-0
lines changed

5 files changed

+383
-0
lines changed

src/redis/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:22.12-alpine as builder
2+
3+
COPY src/redis /app
4+
5+
WORKDIR /app
6+
7+
RUN --mount=type=cache,target=/root/.npm npm install
8+
9+
RUN npm run build
10+
11+
FROM node:22-alpine AS release
12+
13+
COPY --from=builder /app/build /app/build
14+
COPY --from=builder /app/package.json /app/package.json
15+
COPY --from=builder /app/package-lock.json /app/package-lock.json
16+
17+
ENV NODE_ENV=production
18+
19+
WORKDIR /app
20+
21+
RUN npm ci --ignore-scripts --omit-dev
22+
23+
ENTRYPOINT ["node", "build/index.js"]

src/redis/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Redis
2+
3+
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.
4+
5+
## Components
6+
7+
### Tools
8+
9+
- **set**
10+
- Set a Redis key-value pair with optional expiration
11+
- Input:
12+
- `key` (string): Redis key
13+
- `value` (string): Value to store
14+
- `expireSeconds` (number, optional): Expiration time in seconds
15+
16+
- **get**
17+
- Get value by key from Redis
18+
- Input: `key` (string): Redis key to retrieve
19+
20+
- **delete**
21+
- Delete one or more keys from Redis
22+
- Input: `key` (string | string[]): Key or array of keys to delete
23+
24+
- **list**
25+
- List Redis keys matching a pattern
26+
- Input: `pattern` (string, optional): Pattern to match keys (default: *)
27+
28+
## Usage with Claude Desktop
29+
30+
To use this server with the Claude Desktop app, add the following configuration to the "mcpServers" section of your `claude_desktop_config.json`:
31+
32+
### Docker
33+
34+
* when running docker on macos, use host.docker.internal if the server is running on the host network (eg localhost)
35+
* Redis URL can be specified as an argument, defaults to "redis://localhost:6379"
36+
37+
```json
38+
{
39+
"mcpServers": {
40+
"redis": {
41+
"command": "docker",
42+
"args": [
43+
"run",
44+
"-i",
45+
"--rm",
46+
"mcp/redis",
47+
"redis://host.docker.internal:6379"]
48+
}
49+
}
50+
}
51+
```
52+
53+
### NPX
54+
55+
```json
56+
{
57+
"mcpServers": {
58+
"redis": {
59+
"command": "npx",
60+
"args": [
61+
"-y",
62+
"@modelcontextprotocol/server-redis",
63+
"redis://localhost:6379"
64+
]
65+
}
66+
}
67+
}
68+
```
69+
70+
## Building
71+
72+
Docker:
73+
74+
```sh
75+
docker build -t mcp/redis -f src/redis/Dockerfile .
76+
```
77+
78+
## License
79+
80+
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.

src/redis/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "redis",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "module",
6+
"bin": {
7+
"redis": "./build/index.js"
8+
},
9+
"scripts": {
10+
"build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\""
11+
},
12+
"files": [
13+
"build"
14+
],
15+
"keywords": [],
16+
"author": "",
17+
"license": "ISC",
18+
"description": "",
19+
"devDependencies": {
20+
"@types/node": "^22.10.2",
21+
"typescript": "^5.7.2"
22+
},
23+
"dependencies": {
24+
"@modelcontextprotocol/sdk": "^0.4.0",
25+
"@types/redis": "^4.0.10",
26+
"redis": "^4.7.0"
27+
}
28+
}

src/redis/src/index.ts

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3+
import {
4+
CallToolRequestSchema,
5+
ListToolsRequestSchema,
6+
} from "@modelcontextprotocol/sdk/types.js";
7+
import { z } from "zod";
8+
import { createClient } from 'redis';
9+
10+
// Get Redis URL from command line args or use default
11+
const REDIS_URL = process.argv[2] || "redis://localhost:6379";
12+
const redisClient = createClient({
13+
url: REDIS_URL
14+
});
15+
16+
// Define Zod schemas for validation
17+
const SetArgumentsSchema = z.object({
18+
key: z.string(),
19+
value: z.string(),
20+
expireSeconds: z.number().optional(),
21+
});
22+
23+
const GetArgumentsSchema = z.object({
24+
key: z.string(),
25+
});
26+
27+
const DeleteArgumentsSchema = z.object({
28+
key: z.string().or(z.array(z.string())),
29+
});
30+
31+
const ListArgumentsSchema = z.object({
32+
pattern: z.string().default("*"),
33+
});
34+
35+
// Create server instance
36+
const server = new Server(
37+
{
38+
name: "redis",
39+
version: "1.0.0"
40+
}
41+
);
42+
43+
// List available tools
44+
server.setRequestHandler(ListToolsRequestSchema, async () => {
45+
return {
46+
tools: [
47+
{
48+
name: "set",
49+
description: "Set a Redis key-value pair with optional expiration",
50+
inputSchema: {
51+
type: "object",
52+
properties: {
53+
key: {
54+
type: "string",
55+
description: "Redis key",
56+
},
57+
value: {
58+
type: "string",
59+
description: "Value to store",
60+
},
61+
expireSeconds: {
62+
type: "number",
63+
description: "Optional expiration time in seconds",
64+
},
65+
},
66+
required: ["key", "value"],
67+
},
68+
},
69+
{
70+
name: "get",
71+
description: "Get value by key from Redis",
72+
inputSchema: {
73+
type: "object",
74+
properties: {
75+
key: {
76+
type: "string",
77+
description: "Redis key to retrieve",
78+
},
79+
},
80+
required: ["key"],
81+
},
82+
},
83+
{
84+
name: "delete",
85+
description: "Delete one or more keys from Redis",
86+
inputSchema: {
87+
type: "object",
88+
properties: {
89+
key: {
90+
oneOf: [
91+
{ type: "string" },
92+
{ type: "array", items: { type: "string" } }
93+
],
94+
description: "Key or array of keys to delete",
95+
},
96+
},
97+
required: ["key"],
98+
},
99+
},
100+
{
101+
name: "list",
102+
description: "List Redis keys matching a pattern",
103+
inputSchema: {
104+
type: "object",
105+
properties: {
106+
pattern: {
107+
type: "string",
108+
description: "Pattern to match keys (default: *)",
109+
},
110+
},
111+
},
112+
},
113+
],
114+
};
115+
});
116+
117+
// Handle tool execution
118+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
119+
const { name, arguments: args } = request.params;
120+
121+
try {
122+
if (name === "set") {
123+
const { key, value, expireSeconds } = SetArgumentsSchema.parse(args);
124+
125+
if (expireSeconds) {
126+
await redisClient.setEx(key, expireSeconds, value);
127+
} else {
128+
await redisClient.set(key, value);
129+
}
130+
131+
return {
132+
content: [
133+
{
134+
type: "text",
135+
text: `Successfully set key: ${key}`,
136+
},
137+
],
138+
};
139+
} else if (name === "get") {
140+
const { key } = GetArgumentsSchema.parse(args);
141+
const value = await redisClient.get(key);
142+
143+
if (value === null) {
144+
return {
145+
content: [
146+
{
147+
type: "text",
148+
text: `Key not found: ${key}`,
149+
},
150+
],
151+
};
152+
}
153+
154+
return {
155+
content: [
156+
{
157+
type: "text",
158+
text: `${value}`,
159+
},
160+
],
161+
};
162+
} else if (name === "delete") {
163+
const { key } = DeleteArgumentsSchema.parse(args);
164+
165+
if (Array.isArray(key)) {
166+
await redisClient.del(key);
167+
return {
168+
content: [
169+
{
170+
type: "text",
171+
text: `Successfully deleted ${key.length} keys`,
172+
},
173+
],
174+
};
175+
} else {
176+
await redisClient.del(key);
177+
return {
178+
content: [
179+
{
180+
type: "text",
181+
text: `Successfully deleted key: ${key}`,
182+
},
183+
],
184+
};
185+
}
186+
} else if (name === "list") {
187+
const { pattern } = ListArgumentsSchema.parse(args);
188+
const keys = await redisClient.keys(pattern);
189+
190+
return {
191+
content: [
192+
{
193+
type: "text",
194+
text: keys.length > 0
195+
? `Found keys:\n${keys.join('\n')}`
196+
: "No keys found matching pattern",
197+
},
198+
],
199+
};
200+
} else {
201+
throw new Error(`Unknown tool: ${name}`);
202+
}
203+
} catch (error) {
204+
if (error instanceof z.ZodError) {
205+
throw new Error(
206+
`Invalid arguments: ${error.errors
207+
.map((e) => `${e.path.join(".")}: ${e.message}`)
208+
.join(", ")}`
209+
);
210+
}
211+
throw error;
212+
}
213+
});
214+
215+
// Start the server
216+
async function main() {
217+
try {
218+
// Connect to Redis
219+
redisClient.on('error', (err: Error) => console.error('Redis Client Error', err));
220+
await redisClient.connect();
221+
console.error(`Connected to Redis successfully at ${REDIS_URL}`);
222+
223+
const transport = new StdioServerTransport();
224+
await server.connect(transport);
225+
console.error("Redis MCP Server running on stdio");
226+
} catch (error) {
227+
console.error("Error during startup:", error);
228+
await redisClient.quit();
229+
process.exit(1);
230+
}
231+
}
232+
233+
main().catch((error) => {
234+
console.error("Fatal error in main():", error);
235+
redisClient.quit().finally(() => process.exit(1));
236+
});

src/redis/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
16+

0 commit comments

Comments
 (0)