Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion packages/core/src/mcpresource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
SecretDetectionOptions,
WorkspaceFile,
} from "./types.js";
import type { McpClientManager } from "./mcpclient.js";

export interface ResourceContent {
uri: string; // The URI of the resource
Expand All @@ -37,8 +38,39 @@ export interface Resource {

export class ResourceManager extends EventTarget {
private _resources: Record<string, Resource> = {};
private _mcpClientManager?: McpClientManager; // Will be set after construction

setMcpClientManager(mcpClientManager: McpClientManager) {
this._mcpClientManager = mcpClientManager;
}

async resources(): Promise<ResourceReference[]> {
return Object.values(this._resources).map((r) => r.reference);
const localResources = Object.values(this._resources).map((r) => r.reference);

// Also get resources from all connected MCP servers
const mcpResources: ResourceReference[] = [];
if (this._mcpClientManager) {
try {
const clients = this._mcpClientManager.clients || [];
for (const client of clients) {
try {
const serverResources = await client.listResources();
mcpResources.push(...serverResources.map((r: any) => ({
name: r.name,
description: r.description,
uri: r.uri,
mimeType: r.mimeType,
} satisfies ResourceReference)));
} catch (error) {
dbg(`error listing resources from MCP server ${client.config?.id}: ${error instanceof Error ? error.message : String(error)}`);
}
}
} catch (error) {
dbg(`error accessing MCP clients: ${error instanceof Error ? error.message : String(error)}`);
}
}

return [...localResources, ...mcpResources];
}
async readResource(uri: string): Promise<ResourceContents | undefined> {
dbg(`reading resource: ${uri}`);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/testhost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
import { defaultModelConfigurations } from "./llms.js";
import type { CancellationToken } from "./cancellation.js";
import { createNodePath } from "./path.js";
import type { McpClientManager } from "./mcpclient.js";
import { McpClientManager } from "./mcpclient.js";
import { ResourceManager } from "./mcpresource.js";
import { execSync, spawn } from "node:child_process";
import { shellQuote } from "./shell.js";
Expand Down Expand Up @@ -74,7 +74,9 @@ export class TestHost implements RuntimeHost {
}

constructor() {
this.mcp = new McpClientManager();
this.resources = new ResourceManager();
this.resources.setMcpClientManager(this.mcp);
}

async pullModel(
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class NodeHost extends EventTarget implements RuntimeHost {
);
this.mcp = new McpClientManager();
this.resources = new ResourceManager();
this.resources.setMcpClientManager(this.mcp);
this.workspace = createWorkspaceFileSystem();
}

Expand Down