| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { SvelteMap } from 'svelte/reactivity'; |
| import { AttachmentType } from '$lib/enums'; |
| import { |
| MCP_RESOURCE_ATTACHMENT_ID_PREFIX, |
| MCP_RESOURCE_CACHE_MAX_ENTRIES, |
| MCP_RESOURCE_CACHE_TTL_MS, |
| NEWLINE_SEPARATOR, |
| RESOURCE_UNKNOWN_TYPE, |
| BINARY_CONTENT_LABEL |
| } from '$lib/constants'; |
| import { normalizeResourceUri } from '$lib/utils'; |
| import type { |
| MCPResource, |
| MCPResourceTemplate, |
| MCPResourceContent, |
| MCPResourceInfo, |
| MCPResourceTemplateInfo, |
| MCPCachedResource, |
| MCPResourceAttachment, |
| MCPResourceSubscription, |
| MCPServerResources, |
| DatabaseMessageExtraMcpResource |
| } from '$lib/types'; |
|
|
| function generateAttachmentId(): string { |
| return `${MCP_RESOURCE_ATTACHMENT_ID_PREFIX}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`; |
| } |
|
|
| class MCPResourceStore { |
| private _serverResources = $state<SvelteMap<string, MCPServerResources>>(new SvelteMap()); |
| private _cachedResources = $state<SvelteMap<string, MCPCachedResource>>(new SvelteMap()); |
| private _subscriptions = $state<SvelteMap<string, MCPResourceSubscription>>(new SvelteMap()); |
| private _attachments = $state<MCPResourceAttachment[]>([]); |
| private _isLoading = $state(false); |
|
|
| get serverResources(): Map<string, MCPServerResources> { |
| return this._serverResources; |
| } |
|
|
| get cachedResources(): Map<string, MCPCachedResource> { |
| return this._cachedResources; |
| } |
|
|
| get subscriptions(): Map<string, MCPResourceSubscription> { |
| return this._subscriptions; |
| } |
|
|
| get attachments(): MCPResourceAttachment[] { |
| return this._attachments; |
| } |
|
|
| get isLoading(): boolean { |
| return this._isLoading; |
| } |
|
|
| get totalResourceCount(): number { |
| let count = 0; |
| for (const serverRes of this._serverResources.values()) { |
| count += serverRes.resources.length; |
| } |
|
|
| return count; |
| } |
|
|
| get totalTemplateCount(): number { |
| let count = 0; |
| for (const serverRes of this._serverResources.values()) { |
| count += serverRes.templates.length; |
| } |
|
|
| return count; |
| } |
|
|
| get attachmentCount(): number { |
| return this._attachments.length; |
| } |
|
|
| get hasAttachments(): boolean { |
| return this._attachments.length > 0; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| setServerResources( |
| serverName: string, |
| resources: MCPResource[], |
| templates: MCPResourceTemplate[] |
| ): void { |
| this._serverResources.set(serverName, { |
| serverName, |
| resources, |
| templates, |
| lastFetched: new Date(), |
| loading: false, |
| error: undefined |
| }); |
| console.log( |
| `[MCPResources][${serverName}] Set ${resources.length} resources, ${templates.length} templates` |
| ); |
| } |
|
|
| |
| |
| |
| setServerLoading(serverName: string, loading: boolean): void { |
| const existing = this._serverResources.get(serverName); |
| if (existing) { |
| this._serverResources.set(serverName, { ...existing, loading }); |
| } else { |
| this._serverResources.set(serverName, { |
| serverName, |
| resources: [], |
| templates: [], |
| loading, |
| error: undefined |
| }); |
| } |
| } |
|
|
| |
| |
| |
| setServerError(serverName: string, error: string): void { |
| const existing = this._serverResources.get(serverName); |
|
|
| if (existing) { |
| this._serverResources.set(serverName, { ...existing, loading: false, error }); |
| } else { |
| this._serverResources.set(serverName, { |
| serverName, |
| resources: [], |
| templates: [], |
| loading: false, |
| error |
| }); |
| } |
| } |
|
|
| |
| |
| |
| getServerResources(serverName: string): MCPServerResources | undefined { |
| return this._serverResources.get(serverName); |
| } |
|
|
| |
| |
| |
| getAllResourceInfos(): MCPResourceInfo[] { |
| const result: MCPResourceInfo[] = []; |
|
|
| for (const [serverName, serverRes] of this._serverResources) { |
| for (const resource of serverRes.resources) { |
| result.push({ |
| uri: resource.uri, |
| name: resource.name, |
| title: resource.title, |
| description: resource.description, |
| mimeType: resource.mimeType, |
| serverName, |
| annotations: resource.annotations, |
| icons: resource.icons |
| }); |
| } |
| } |
|
|
| return result; |
| } |
|
|
| |
| |
| |
| getAllTemplateInfos(): MCPResourceTemplateInfo[] { |
| const result: MCPResourceTemplateInfo[] = []; |
|
|
| for (const [serverName, serverRes] of this._serverResources) { |
| for (const template of serverRes.templates) { |
| result.push({ |
| uriTemplate: template.uriTemplate, |
| name: template.name, |
| title: template.title, |
| description: template.description, |
| mimeType: template.mimeType, |
| serverName, |
| annotations: template.annotations, |
| icons: template.icons |
| }); |
| } |
| } |
|
|
| return result; |
| } |
|
|
| |
| |
| |
| clearServerResources(serverName: string): void { |
| this._serverResources.delete(serverName); |
|
|
| |
| for (const [uri, cached] of this._cachedResources) { |
| if (cached.resource.serverName === serverName) { |
| this._cachedResources.delete(uri); |
| } |
| } |
|
|
| |
| for (const [uri, sub] of this._subscriptions) { |
| if (sub.serverName === serverName) { |
| this._subscriptions.delete(uri); |
| } |
| } |
|
|
| console.log(`[MCPResources][${serverName}] Cleared all resources`); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| cacheResourceContent(resource: MCPResourceInfo, content: MCPResourceContent[]): void { |
| |
| if (this._cachedResources.size >= MCP_RESOURCE_CACHE_MAX_ENTRIES) { |
| |
| const oldestKey = this._cachedResources.keys().next().value; |
|
|
| if (oldestKey) { |
| this._cachedResources.delete(oldestKey); |
| } |
| } |
|
|
| this._cachedResources.set(resource.uri, { |
| resource, |
| content, |
| fetchedAt: new Date(), |
| subscribed: this._subscriptions.has(resource.uri) |
| }); |
| console.log(`[MCPResources] Cached content for: ${resource.uri}`); |
| } |
|
|
| |
| |
| |
| getCachedContent(uri: string): MCPCachedResource | undefined { |
| const cached = this._cachedResources.get(uri); |
| if (!cached) return undefined; |
|
|
| |
| const age = Date.now() - cached.fetchedAt.getTime(); |
|
|
| if (age > MCP_RESOURCE_CACHE_TTL_MS && !cached.subscribed) { |
| |
| this._cachedResources.delete(uri); |
|
|
| return undefined; |
| } |
|
|
| return cached; |
| } |
|
|
| |
| |
| |
| invalidateCache(uri: string): void { |
| this._cachedResources.delete(uri); |
| console.log(`[MCPResources] Invalidated cache for: ${uri}`); |
| } |
|
|
| |
| |
| |
| clearCache(): void { |
| this._cachedResources.clear(); |
| console.log(`[MCPResources] Cleared all cached content`); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| addSubscription(uri: string, serverName: string): void { |
| this._subscriptions.set(uri, { |
| uri, |
| serverName, |
| subscribedAt: new Date() |
| }); |
|
|
| |
| const cached = this._cachedResources.get(uri); |
| if (cached) { |
| this._cachedResources.set(uri, { ...cached, subscribed: true }); |
| } |
|
|
| console.log(`[MCPResources] Added subscription: ${uri}`); |
| } |
|
|
| |
| |
| |
| removeSubscription(uri: string): void { |
| this._subscriptions.delete(uri); |
|
|
| |
| const cached = this._cachedResources.get(uri); |
| if (cached) { |
| this._cachedResources.set(uri, { ...cached, subscribed: false }); |
| } |
|
|
| console.log(`[MCPResources] Removed subscription: ${uri}`); |
| } |
|
|
| |
| |
| |
| isSubscribed(uri: string): boolean { |
| return this._subscriptions.has(uri); |
| } |
|
|
| |
| |
| |
| handleResourceUpdate(uri: string): void { |
| |
| this.invalidateCache(uri); |
|
|
| |
| const sub = this._subscriptions.get(uri); |
| if (sub) { |
| this._subscriptions.set(uri, { ...sub, lastUpdate: new Date() }); |
| } |
|
|
| console.log(`[MCPResources] Resource updated: ${uri}`); |
| } |
|
|
| |
| |
| |
| handleResourcesListChanged(serverName: string): void { |
| |
| const existing = this._serverResources.get(serverName); |
| if (existing) { |
| this._serverResources.set(serverName, { |
| ...existing, |
| lastFetched: undefined |
| }); |
| } |
| console.log(`[MCPResources][${serverName}] Resources list changed, needs refresh`); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| addAttachment(resource: MCPResourceInfo): MCPResourceAttachment { |
| const attachment: MCPResourceAttachment = { |
| id: generateAttachmentId(), |
| resource, |
| loading: true |
| }; |
|
|
| this._attachments = [...this._attachments, attachment]; |
| console.log(`[MCPResources] Added attachment: ${resource.uri}`); |
|
|
| return attachment; |
| } |
|
|
| |
| |
| |
| updateAttachmentContent(attachmentId: string, content: MCPResourceContent[]): void { |
| this._attachments = this._attachments.map((att) => |
| att.id === attachmentId ? { ...att, content, loading: false, error: undefined } : att |
| ); |
| } |
|
|
| |
| |
| |
| updateAttachmentError(attachmentId: string, error: string): void { |
| this._attachments = this._attachments.map((att) => |
| att.id === attachmentId ? { ...att, loading: false, error } : att |
| ); |
| } |
|
|
| |
| |
| |
| removeAttachment(attachmentId: string): void { |
| this._attachments = this._attachments.filter((att) => att.id !== attachmentId); |
| console.log(`[MCPResources] Removed attachment: ${attachmentId}`); |
| } |
|
|
| |
| |
| |
| clearAttachments(): void { |
| this._attachments = []; |
| console.log(`[MCPResources] Cleared all attachments`); |
| } |
|
|
| |
| |
| |
| getAttachment(attachmentId: string): MCPResourceAttachment | undefined { |
| return this._attachments.find((att) => att.id === attachmentId); |
| } |
|
|
| |
| |
| |
| isAttached(uri: string): boolean { |
| const normalizedUri = normalizeResourceUri(uri); |
|
|
| return this._attachments.some( |
| (att) => att.resource.uri === uri || normalizeResourceUri(att.resource.uri) === normalizedUri |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| setLoading(loading: boolean): void { |
| this._isLoading = loading; |
| } |
|
|
| |
| |
| |
| findResourceByUri(uri: string): MCPResourceInfo | undefined { |
| const normalizedUri = normalizeResourceUri(uri); |
|
|
| for (const [serverName, serverRes] of this._serverResources) { |
| const resource = |
| serverRes.resources.find((r) => r.uri === uri) ?? |
| serverRes.resources.find((r) => normalizeResourceUri(r.uri) === normalizedUri); |
|
|
| if (resource) { |
| return { |
| uri: resource.uri, |
| name: resource.name, |
| title: resource.title, |
| description: resource.description, |
| mimeType: resource.mimeType, |
| serverName, |
| annotations: resource.annotations, |
| icons: resource.icons |
| }; |
| } |
| } |
|
|
| return undefined; |
| } |
|
|
| |
| |
| |
| findServerForUri(uri: string): string | undefined { |
| for (const [serverName, serverRes] of this._serverResources) { |
| if (serverRes.resources.some((r) => r.uri === uri)) { |
| return serverName; |
| } |
| } |
|
|
| return undefined; |
| } |
|
|
| |
| |
| |
| clear(): void { |
| this._serverResources.clear(); |
| this._cachedResources.clear(); |
| this._subscriptions.clear(); |
| this._attachments = []; |
| this._isLoading = false; |
| console.log(`[MCPResources] Cleared all state`); |
| } |
|
|
| |
| |
| |
| |
| formatAttachmentsForContext(): string { |
| if (this._attachments.length === 0) return ''; |
|
|
| const parts: string[] = []; |
|
|
| for (const attachment of this._attachments) { |
| if (attachment.error) continue; |
| if (!attachment.content || attachment.content.length === 0) continue; |
|
|
| const resourceName = attachment.resource.title || attachment.resource.name; |
| const serverName = attachment.resource.serverName; |
|
|
| for (const content of attachment.content) { |
| if ('text' in content && content.text) { |
| parts.push(`\n\n--- Resource: ${resourceName} (from ${serverName}) ---\n${content.text}`); |
| } else if ('blob' in content && content.blob) { |
| |
| parts.push( |
| `\n\n--- Resource: ${resourceName} (from ${serverName}) ---\n[${BINARY_CONTENT_LABEL}: ${content.mimeType || RESOURCE_UNKNOWN_TYPE}]` |
| ); |
| } |
| } |
| } |
|
|
| return parts.join(''); |
| } |
|
|
| |
| |
| |
| |
| toMessageExtras(): DatabaseMessageExtraMcpResource[] { |
| const extras: DatabaseMessageExtraMcpResource[] = []; |
|
|
| for (const attachment of this._attachments) { |
| if (attachment.error) continue; |
| if (!attachment.content || attachment.content.length === 0) continue; |
|
|
| const resourceName = attachment.resource.title || attachment.resource.name; |
| const contentParts: string[] = []; |
|
|
| for (const content of attachment.content) { |
| if ('text' in content && content.text) { |
| contentParts.push(content.text); |
| } else if ('blob' in content && content.blob) { |
| contentParts.push( |
| `[${BINARY_CONTENT_LABEL}: ${content.mimeType || RESOURCE_UNKNOWN_TYPE}]` |
| ); |
| } |
| } |
|
|
| if (contentParts.length > 0) { |
| extras.push({ |
| type: AttachmentType.MCP_RESOURCE, |
| name: resourceName, |
| uri: attachment.resource.uri, |
| serverName: attachment.resource.serverName, |
| content: contentParts.join(NEWLINE_SEPARATOR), |
| mimeType: attachment.resource.mimeType |
| }); |
| } |
| } |
|
|
| return extras; |
| } |
| } |
|
|
| export const mcpResourceStore = new MCPResourceStore(); |
|
|
| |
| export const mcpResources = () => mcpResourceStore.serverResources; |
| export const mcpResourceAttachments = () => mcpResourceStore.attachments; |
| export const mcpResourceAttachmentCount = () => mcpResourceStore.attachmentCount; |
| export const mcpHasResourceAttachments = () => mcpResourceStore.hasAttachments; |
| export const mcpTotalResourceCount = () => mcpResourceStore.totalResourceCount; |
| export const mcpResourcesLoading = () => mcpResourceStore.isLoading; |
|
|