Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FastifyInstance } from 'fastify'
import fp from 'fastify-plugin'
import { Redis } from 'ioredis'
import { Redis, type RedisOptions } from 'ioredis'
import type { SessionStore } from './stores/session-store.ts'
import type { MessageBroker } from './brokers/message-broker.ts'
import { MemorySessionStore } from './stores/memory-session-store.ts'
Expand Down Expand Up @@ -34,6 +34,20 @@ import type {
GetPromptResult
} from './schema.ts'

function isIoRedis (value: unknown): value is Redis {
if (typeof value !== 'object' || value === null) return false
// can match if the same module is loaded
if (value instanceof Redis) return true
// otherwise treat as a duck type, which can be useful for mocking anyhow
const v = value as Partial<Redis>
return (
typeof v.connect === 'function' &&
typeof v.quit === 'function' &&
typeof v.hset === 'function' &&
typeof v.get === 'function'
)
}

const mcpPlugin = fp(async function (app: FastifyInstance, opts: MCPPluginOptions) {
const serverInfo: Implementation = opts.serverInfo ?? {
name: '@platformatic/mcp',
Expand All @@ -55,10 +69,15 @@ const mcpPlugin = fp(async function (app: FastifyInstance, opts: MCPPluginOption
let sessionStore: SessionStore
let messageBroker: MessageBroker
let redis: Redis | null = null
let redisIsInternallyManaged = false

if (opts.redis) {
// Redis implementations for horizontal scaling
redis = new Redis(opts.redis)
if (isIoRedis(opts.redis)) {
redis = opts.redis
} else {
redis = new Redis(opts.redis as RedisOptions) // or string, to overcome type narrowing
redisIsInternallyManaged = true
}
sessionStore = new RedisSessionStore({ redis, maxMessages: 100 })
messageBroker = new RedisMessageBroker(redis)
} else {
Expand Down Expand Up @@ -144,7 +163,7 @@ const mcpPlugin = fp(async function (app: FastifyInstance, opts: MCPPluginOption
// Execute all unsubscribes in parallel
await Promise.all(unsubscribePromises)

if (redis) {
if (redis && redisIsInternallyManaged) {
await redis.quit()
}
await messageBroker.close()
Expand Down
8 changes: 2 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
RequestId
} from './schema.ts'
import type { Static, TSchema, TObject, TString } from '@sinclair/typebox'
import type { Redis, RedisOptions } from 'ioredis'
import type { AuthorizationConfig, AuthorizationContext } from './types/auth-types.ts'

// Context interface for all handler types
Expand Down Expand Up @@ -138,12 +139,7 @@ export interface MCPPluginOptions {
enableSSE?: boolean
sessionStore?: 'memory' | 'redis'
messageBroker?: 'memory' | 'redis'
redis?: {
host: string
port: number
password?: string
db?: number
}
redis?: Redis | RedisOptions | string
authorization?: AuthorizationConfig
}

Expand Down
47 changes: 47 additions & 0 deletions test/redis-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fastify from 'fastify'
import mcpPlugin from '../src/index.ts'
import { testWithRedis } from './redis-test-utils.ts'
import type { JSONRPCMessage } from '../src/schema.ts'
import { Redis } from 'ioredis'

describe('Redis Integration Tests', () => {
testWithRedis('should initialize plugin with Redis configuration', async (redis, t) => {
Expand All @@ -27,6 +28,52 @@ describe('Redis Integration Tests', () => {
assert.ok(app.mcpSendToSession)
})

testWithRedis('should initialize plugin with a Redis client', async (redis, t) => {
const app = fastify()

const client = new Redis({
host: redis.options.host!,
port: redis.options.port!,
db: redis.options.db!
})

t.after(async () => {
await app.close()
await client.quit()
})

await app.register(mcpPlugin, {
enableSSE: true,
redis: client
})

// Verify plugin is registered
assert.ok(app.mcpAddTool)
assert.ok(app.mcpAddResource)
assert.ok(app.mcpAddPrompt)
assert.ok(app.mcpBroadcastNotification)
assert.ok(app.mcpSendToSession)
})

testWithRedis('should initialize plugin with a Redis url', async (redis, t) => {
const app = fastify()
t.after(() => app.close())

const url = `redis://${redis.options.host}:${redis.options.port}/${redis.options.db}`

await app.register(mcpPlugin, {
enableSSE: true,
redis: url
})

// Verify plugin is registered
assert.ok(app.mcpAddTool)
assert.ok(app.mcpAddResource)
assert.ok(app.mcpAddPrompt)
assert.ok(app.mcpBroadcastNotification)
assert.ok(app.mcpSendToSession)
})

testWithRedis('should handle MCP requests with Redis backend', async (redis, t) => {
const app = fastify()
t.after(() => app.close())
Expand Down
Loading