|
| 1 | +import { Server } from "../server.js"; |
| 2 | +import { Session } from "../common/session.js"; |
| 3 | +import { UserConfig } from "../common/config.js"; |
| 4 | +import { Telemetry } from "../telemetry/telemetry.js"; |
| 5 | +import type { SessionEvents } from "../common/session.js"; |
| 6 | +import { ReadResourceCallback, ResourceMetadata } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 7 | +import logger, { LogId } from "../common/logger.js"; |
| 8 | + |
| 9 | +type PayloadOf<K extends keyof SessionEvents> = SessionEvents[K][0]; |
| 10 | + |
| 11 | +type ResourceConfiguration = { name: string; uri: string; config: ResourceMetadata }; |
| 12 | + |
| 13 | +export function ReactiveResource<Value, RelevantEvents extends readonly (keyof SessionEvents)[]>( |
| 14 | + { name, uri, config: resourceConfig }: ResourceConfiguration, |
| 15 | + { |
| 16 | + initial, |
| 17 | + events, |
| 18 | + }: { |
| 19 | + initial: Value; |
| 20 | + events: RelevantEvents; |
| 21 | + } |
| 22 | +) { |
| 23 | + type SomeEvent = RelevantEvents[number]; |
| 24 | + |
| 25 | + abstract class NewReactiveResource { |
| 26 | + protected readonly session: Session; |
| 27 | + protected readonly config: UserConfig; |
| 28 | + protected current: Value; |
| 29 | + |
| 30 | + constructor( |
| 31 | + protected readonly server: Server, |
| 32 | + protected readonly telemetry: Telemetry, |
| 33 | + current?: Value |
| 34 | + ) { |
| 35 | + this.current = current ?? initial; |
| 36 | + this.session = server.session; |
| 37 | + this.config = server.userConfig; |
| 38 | + |
| 39 | + for (const event of events) { |
| 40 | + this.session.on(event, (...args: SessionEvents[typeof event]) => { |
| 41 | + this.reduceApply(event, (args as unknown[])[0] as PayloadOf<typeof event>); |
| 42 | + void this.triggerUpdate(); |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public register(): void { |
| 48 | + this.server.mcpServer.registerResource(name, uri, resourceConfig, this.resourceCallback); |
| 49 | + } |
| 50 | + |
| 51 | + private resourceCallback: ReadResourceCallback = (uri) => ({ |
| 52 | + contents: [ |
| 53 | + { |
| 54 | + text: this.toOutput(), |
| 55 | + mimeType: "application/json", |
| 56 | + uri: uri.href, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }); |
| 60 | + |
| 61 | + private async triggerUpdate() { |
| 62 | + try { |
| 63 | + await this.server.mcpServer.server.sendResourceUpdated({ uri }); |
| 64 | + this.server.mcpServer.sendResourceListChanged(); |
| 65 | + } catch (error: unknown) { |
| 66 | + logger.warning( |
| 67 | + LogId.serverClosed, |
| 68 | + "Could not send the latest resources to the client.", |
| 69 | + error as string |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + reduceApply(eventName: SomeEvent, ...event: PayloadOf<SomeEvent>[]): void { |
| 75 | + this.current = this.reduce(eventName, ...event); |
| 76 | + } |
| 77 | + |
| 78 | + protected abstract reduce(eventName: SomeEvent, ...event: PayloadOf<SomeEvent>[]): Value; |
| 79 | + abstract toOutput(): string; |
| 80 | + } |
| 81 | + |
| 82 | + return NewReactiveResource; |
| 83 | +} |
0 commit comments