Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 21 additions & 3 deletions src/vs/workbench/contrib/mcp/common/mcpResourceFilesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import { equalsIgnoreCase } from '../../../../base/common/strings.js';
import { URI } from '../../../../base/common/uri.js';
import { createFileSystemProviderError, FileChangeType, FileSystemProviderCapabilities, FileSystemProviderErrorCode, FileType, IFileChange, IFileDeleteOptions, IFileOverwriteOptions, IFileReadStreamOptions, IFileService, IFileSystemProviderWithFileAtomicReadCapability, IFileSystemProviderWithFileReadStreamCapability, IFileSystemProviderWithFileReadWriteCapability, IFileWriteOptions, IStat, IWatchOptions } from '../../../../platform/files/common/files.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IWebContentExtractorService } from '../../../../platform/webContentExtractor/common/webContentExtractor.js';
import { IWorkbenchContribution } from '../../../common/contributions.js';
import { McpServer } from './mcpServer.js';
import { McpServerRequestHandler } from './mcpServerRequestHandler.js';
import { IMcpService, McpCapability, McpResourceURI } from './mcpTypes.js';
import { ValidateHttpResources } from './mcpTypesUtils.js';
import { MCP } from './modelContextProtocol.js';

const MOMENTARY_CACHE_DURATION = 3000;
Expand Down Expand Up @@ -65,6 +67,7 @@ export class McpResourceFilesystem extends Disposable implements IWorkbenchContr
constructor(
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IFileService private readonly _fileService: IFileService,
@IWebContentExtractorService private readonly _webContentExtractorService: IWebContentExtractorService,
) {
super();
this._register(this._fileService.registerProvider(McpResourceURI.scheme, this));
Expand Down Expand Up @@ -164,7 +167,6 @@ export class McpResourceFilesystem extends Disposable implements IWorkbenchContr
if (forSameURI.length > 0) {
throw createFileSystemProviderError(`File is not a directory`, FileSystemProviderErrorCode.FileNotADirectory);
}

const resourcePathParts = resourceURI.pathname.split('/');

const output = new Map<string, FileType>();
Expand Down Expand Up @@ -273,12 +275,28 @@ export class McpResourceFilesystem extends Disposable implements IWorkbenchContr

private async _readURIInner(uri: URI, token?: CancellationToken): Promise<IReadData> {
const { resourceURI, server } = this._decodeURI(uri);
const matchedServer = this._mcpService.servers.get().find(s => s.definition.id === server.definition.id);
//check for http/https resources and use web content extractor service to fetch the contents.
if (ValidateHttpResources(uri, matchedServer)) {
const extractURI = URI.parse(resourceURI.toString());
const result = await this._webContentExtractorService.extract([extractURI], { followRedirects: false });
return {
contents: result.map(r => {
if (r.status === 'ok') {
return { uri: resourceURI.toString(), text: r.result };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the status is not okay, we should fall back to requesting the resource from the MCP server

} else {
return { uri: resourceURI.toString(), text: '' };
}
}),
resourceURI,
forSameURI: result.filter(r => r.status === 'ok').map(r => ({ uri: resourceURI.toString(), text: r.result }))
};
}
const res = await McpServer.callOn(server, r => r.readResource({ uri: resourceURI.toString() }, token), token);

return {
contents: res.contents,
resourceURI,
forSameURI: res.contents.filter(c => equalsUrlPath(c.uri, resourceURI)),
forSameURI: res.contents.filter(c => equalsUrlPath(c.uri, resourceURI))
};
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/vs/workbench/contrib/mcp/common/mcpTypesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { URI } from '../../../../base/common/uri.js';
import { disposableTimeout, timeout } from '../../../../base/common/async.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { CancellationError } from '../../../../base/common/errors.js';
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { autorun } from '../../../../base/common/observable.js';
import { ToolDataSource } from '../../chat/common/languageModelToolsService.js';
import { IMcpServer, IMcpServerStartOpts, IMcpService, McpConnectionState, McpServerCacheState } from './mcpTypes.js';
import { IMcpServer, IMcpServerStartOpts, IMcpService, McpConnectionState, McpServerCacheState, McpServerTransportType } from './mcpTypes.js';


/**
* Waits up to `timeout` for a server passing the filter to be discovered,
Expand Down Expand Up @@ -91,3 +93,24 @@ export function mcpServerToSourceData(server: IMcpServer): ToolDataSource {
definitionId: server.definition.id
};
}


/**
* Validates whether the given HTTP or HTTPS resource is allowed for the specified MCP server.
*
* @param resource The URI of the resource to validate.
* @param server The MCP server instance to validate against, or undefined.
* @returns True if the resource request is valid for the server, false otherwise.
*/
export function ValidateHttpResources(resource: URI, server: IMcpServer | undefined) {
let isResourceRequestValid = false;
if (resource.path.startsWith('/http/')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest matching the protocol unpacked resourceURL instead just so you know you're validating against the real URL that's being requested--and avoid the implicit dependency on how the namespaces resource URIs get constructed

const launch = server?.connection.get()?.launchDefinition;
if (launch && launch.type === McpServerTransportType.HTTP && launch.uri.authority.toLowerCase() === resource.authority.toLowerCase()) {
isResourceRequestValid = true;
}
} else if (resource.path.startsWith('/https/')) {
isResourceRequestValid = true;
}
return isResourceRequestValid;
}
Loading