|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { sumBy } from '../../../../base/common/arrays.js'; |
| 7 | +import { decodeBase64, VSBuffer } from '../../../../base/common/buffer.js'; |
| 8 | +import { CancellationToken, CancellationTokenSource } from '../../../../base/common/cancellation.js'; |
| 9 | +import { Emitter, Event } from '../../../../base/common/event.js'; |
| 10 | +import { Lazy } from '../../../../base/common/lazy.js'; |
| 11 | +import { Disposable, DisposableStore, IDisposable, MutableDisposable } from '../../../../base/common/lifecycle.js'; |
| 12 | +import { autorun } from '../../../../base/common/observable.js'; |
| 13 | +import { newWriteableStream, ReadableStreamEvents } from '../../../../base/common/stream.js'; |
| 14 | +import { equalsIgnoreCase } from '../../../../base/common/strings.js'; |
| 15 | +import { URI } from '../../../../base/common/uri.js'; |
| 16 | +import { createFileSystemProviderError, FileChangeType, FileSystemProviderCapabilities, FileSystemProviderErrorCode, FileType, IFileChange, IFileDeleteOptions, IFileOverwriteOptions, IFileReadStreamOptions, IFileService, IFileSystemProviderWithFileAtomicReadCapability, IFileSystemProviderWithFileReadStreamCapability, IFileSystemProviderWithFileReadWriteCapability, IFileWriteOptions, IStat, IWatchOptions } from '../../../../platform/files/common/files.js'; |
| 17 | +import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js'; |
| 18 | +import { IWorkbenchContribution } from '../../../common/contributions.js'; |
| 19 | +import { McpServer } from './mcpServer.js'; |
| 20 | +import { McpServerRequestHandler } from './mcpServerRequestHandler.js'; |
| 21 | +import { IMcpService, McpResourceURI } from './mcpTypes.js'; |
| 22 | +import { MCP } from './modelContextProtocol.js'; |
| 23 | + |
| 24 | +export class McpResourceFilesystem extends Disposable implements IWorkbenchContribution, |
| 25 | + IFileSystemProviderWithFileReadWriteCapability, |
| 26 | + IFileSystemProviderWithFileAtomicReadCapability, |
| 27 | + IFileSystemProviderWithFileReadStreamCapability { |
| 28 | + /** Defer getting the MCP service since this is a BlockRestore and no need to make it unnecessarily. */ |
| 29 | + private readonly _mcpServiceLazy = new Lazy(() => this._instantiationService.invokeFunction(a => a.get(IMcpService))); |
| 30 | + |
| 31 | + private get _mcpService() { |
| 32 | + return this._mcpServiceLazy.value; |
| 33 | + } |
| 34 | + |
| 35 | + public readonly onDidChangeCapabilities = Event.None; |
| 36 | + |
| 37 | + private readonly _onDidChangeFile = this._register(new Emitter<readonly IFileChange[]>()); |
| 38 | + public readonly onDidChangeFile = this._onDidChangeFile.event; |
| 39 | + |
| 40 | + public readonly capabilities: FileSystemProviderCapabilities = FileSystemProviderCapabilities.None |
| 41 | + | FileSystemProviderCapabilities.Readonly |
| 42 | + | FileSystemProviderCapabilities.PathCaseSensitive |
| 43 | + | FileSystemProviderCapabilities.FileReadStream |
| 44 | + | FileSystemProviderCapabilities.FileAtomicRead; |
| 45 | + |
| 46 | + constructor( |
| 47 | + @IInstantiationService private readonly _instantiationService: IInstantiationService, |
| 48 | + @IFileService private readonly _fileService: IFileService, |
| 49 | + ) { |
| 50 | + super(); |
| 51 | + this._register(this._fileService.registerProvider(McpResourceURI.scheme, this)); |
| 52 | + } |
| 53 | + |
| 54 | + //#region Filesystem API |
| 55 | + |
| 56 | + public async readFile(resource: URI): Promise<Uint8Array> { |
| 57 | + return this._readFile(resource); |
| 58 | + } |
| 59 | + |
| 60 | + public readFileStream(resource: URI, opts: IFileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array> { |
| 61 | + const stream = newWriteableStream<Uint8Array>(data => VSBuffer.concat(data.map(data => VSBuffer.wrap(data))).buffer); |
| 62 | + |
| 63 | + this._readFile(resource, token).then( |
| 64 | + data => { |
| 65 | + if (opts.position) { |
| 66 | + data = data.slice(opts.position); |
| 67 | + } |
| 68 | + |
| 69 | + if (opts.length) { |
| 70 | + data = data.slice(0, opts.length); |
| 71 | + } |
| 72 | + |
| 73 | + stream.end(data); |
| 74 | + }, |
| 75 | + err => stream.error(err), |
| 76 | + ); |
| 77 | + |
| 78 | + return stream; |
| 79 | + } |
| 80 | + |
| 81 | + public watch(uri: URI, _opts: IWatchOptions): IDisposable { |
| 82 | + const { resourceURI, server } = this._decodeURI(uri); |
| 83 | + |
| 84 | + server.start(); |
| 85 | + |
| 86 | + const store = new DisposableStore(); |
| 87 | + let watchedOnHandler: McpServerRequestHandler | undefined; |
| 88 | + const watchListener = store.add(new MutableDisposable()); |
| 89 | + const callCts = store.add(new MutableDisposable<CancellationTokenSource>()); |
| 90 | + store.add(autorun(reader => { |
| 91 | + const connection = server.connection.read(reader); |
| 92 | + if (!connection) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const handler = connection.handler.read(reader); |
| 97 | + if (!handler || watchedOnHandler === handler) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + callCts.value?.dispose(true); |
| 102 | + callCts.value = new CancellationTokenSource(); |
| 103 | + watchedOnHandler = handler; |
| 104 | + |
| 105 | + const token = callCts.value.token; |
| 106 | + handler.subscribe({ uri: resourceURI.toString(true) }, token).then( |
| 107 | + () => { |
| 108 | + if (!token.isCancellationRequested) { |
| 109 | + watchListener.value = handler.onDidUpdateResource(e => { |
| 110 | + if (equalsUriPath(e.params.uri, resourceURI)) { |
| 111 | + this._onDidChangeFile.fire([{ resource: uri, type: FileChangeType.UPDATED }]); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + }, err => { |
| 116 | + handler.logger.warn(`Failed to subscribe to resource changes for ${resourceURI}: ${err}`); |
| 117 | + watchedOnHandler = undefined; |
| 118 | + }, |
| 119 | + ); |
| 120 | + })); |
| 121 | + |
| 122 | + return store; |
| 123 | + } |
| 124 | + |
| 125 | + public async stat(resource: URI): Promise<IStat> { |
| 126 | + const { forSameURI, contents } = await this._readURI(resource); |
| 127 | + if (!contents.length) { |
| 128 | + throw createFileSystemProviderError(`File not found`, FileSystemProviderErrorCode.FileNotFound); |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + ctime: 0, |
| 133 | + mtime: 0, |
| 134 | + size: sumBy(contents, c => contentToBuffer(c).byteLength), |
| 135 | + type: forSameURI.length ? FileType.File : FileType.Directory, |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + public async readdir(resource: URI): Promise<[string, FileType][]> { |
| 140 | + const { forSameURI, contents, resourceURI } = await this._readURI(resource); |
| 141 | + if (forSameURI.length > 0) { |
| 142 | + throw createFileSystemProviderError(`File is not a directory`, FileSystemProviderErrorCode.FileNotADirectory); |
| 143 | + } |
| 144 | + |
| 145 | + const resourcePathParts = resourceURI.path.split('/'); |
| 146 | + |
| 147 | + const output = new Map<string, FileType>(); |
| 148 | + for (const content of contents) { |
| 149 | + const contentURI = URI.parse(content.uri); |
| 150 | + const contentPathParts = contentURI.path.split('/'); |
| 151 | + |
| 152 | + // Skip contents that are not in the same directory |
| 153 | + if (contentPathParts.length <= resourcePathParts.length || !resourcePathParts.every((part, index) => equalsIgnoreCase(part, contentPathParts[index]))) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + // nested resource in a directory, just emit a directory to output |
| 158 | + else if (contentPathParts.length > resourcePathParts.length + 1) { |
| 159 | + output.set(contentPathParts[resourcePathParts.length], FileType.Directory); |
| 160 | + } |
| 161 | + |
| 162 | + else { |
| 163 | + // resource in the same directory, emit the file |
| 164 | + const name = contentPathParts[contentPathParts.length - 1]; |
| 165 | + output.set(name, contentToBuffer(content).byteLength > 0 ? FileType.File : FileType.Directory); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return [...output]; |
| 170 | + } |
| 171 | + |
| 172 | + public mkdir(resource: URI): Promise<void> { |
| 173 | + throw createFileSystemProviderError('write is not supported', FileSystemProviderErrorCode.NoPermissions); |
| 174 | + } |
| 175 | + public writeFile(resource: URI, content: Uint8Array, opts: IFileWriteOptions): Promise<void> { |
| 176 | + throw createFileSystemProviderError('write is not supported', FileSystemProviderErrorCode.NoPermissions); |
| 177 | + } |
| 178 | + public delete(resource: URI, opts: IFileDeleteOptions): Promise<void> { |
| 179 | + throw createFileSystemProviderError('delete is not supported', FileSystemProviderErrorCode.NoPermissions); |
| 180 | + } |
| 181 | + public rename(from: URI, to: URI, opts: IFileOverwriteOptions): Promise<void> { |
| 182 | + throw createFileSystemProviderError('rename is not supported', FileSystemProviderErrorCode.NoPermissions); |
| 183 | + } |
| 184 | + |
| 185 | + //#endregion |
| 186 | + |
| 187 | + private async _readFile(resource: URI, token?: CancellationToken): Promise<Uint8Array> { |
| 188 | + const { forSameURI, contents } = await this._readURI(resource); |
| 189 | + |
| 190 | + // MCP does not distinguish between files and directories, and says that |
| 191 | + // servers should just return multiple when 'reading' a directory. |
| 192 | + if (!forSameURI.length) { |
| 193 | + if (!contents.length) { |
| 194 | + throw createFileSystemProviderError(`File not found`, FileSystemProviderErrorCode.FileNotFound); |
| 195 | + } else { |
| 196 | + throw createFileSystemProviderError(`File is a directory`, FileSystemProviderErrorCode.FileIsADirectory); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return contentToBuffer(forSameURI[0]); |
| 201 | + } |
| 202 | + |
| 203 | + private _decodeURI(uri: URI) { |
| 204 | + let definitionId: string; |
| 205 | + let resourceURI: URI; |
| 206 | + try { |
| 207 | + ({ definitionId, resourceURI } = McpResourceURI.toServer(uri)); |
| 208 | + } catch (e) { |
| 209 | + throw createFileSystemProviderError(String(e), FileSystemProviderErrorCode.FileNotFound); |
| 210 | + } |
| 211 | + |
| 212 | + if (resourceURI.path.endsWith('/')) { |
| 213 | + resourceURI = resourceURI.with({ path: resourceURI.path.slice(0, -1) }); |
| 214 | + } |
| 215 | + |
| 216 | + const server = this._mcpService.servers.get().find(s => s.definition.id === definitionId); |
| 217 | + if (!server) { |
| 218 | + throw createFileSystemProviderError(`MCP server with definition ID ${definitionId} not found`, FileSystemProviderErrorCode.FileNotFound); |
| 219 | + } |
| 220 | + |
| 221 | + return { definitionId, resourceURI, server }; |
| 222 | + } |
| 223 | + |
| 224 | + private async _readURI(uri: URI, token?: CancellationToken) { |
| 225 | + const { resourceURI, server } = this._decodeURI(uri); |
| 226 | + const res = await McpServer.callOn(server, r => r.readResource({ uri: resourceURI.toString(true) }, token), token); |
| 227 | + |
| 228 | + return { |
| 229 | + contents: res.contents, |
| 230 | + resourceURI, |
| 231 | + forSameURI: res.contents.filter(c => equalsUriPath(c.uri, resourceURI)), |
| 232 | + }; |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +function equalsUriPath(a: string, b: URI): boolean { |
| 237 | + // MCP doesn't specify either way, but underlying systems may can be case-sensitive. |
| 238 | + // It's better to treat case-sensitive paths as case-insensitive than vise-versa. |
| 239 | + return equalsIgnoreCase(URI.parse(a).path, b.path); |
| 240 | +} |
| 241 | + |
| 242 | +function contentToBuffer(content: MCP.TextResourceContents | MCP.BlobResourceContents): Uint8Array { |
| 243 | + if ('text' in content) { |
| 244 | + return VSBuffer.fromString(content.text).buffer; |
| 245 | + } else if ('blob' in content) { |
| 246 | + return decodeBase64(content.blob).buffer; |
| 247 | + } else { |
| 248 | + throw createFileSystemProviderError('Unknown content type', FileSystemProviderErrorCode.Unknown); |
| 249 | + } |
| 250 | +} |
0 commit comments