Skip to content

Commit 2bd17c6

Browse files
committed
Refactor resource handling: rename resource_read to resources, add resource_list tool, and update documentation. Remove obsolete scripts.
1 parent 19c0fd1 commit 2bd17c6

File tree

6 files changed

+250
-153
lines changed

6 files changed

+250
-153
lines changed

docs/src/components/BuiltinTools.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import { LinkCard } from '@astrojs/starlight/components';
4141
<LinkCard title="python_code_interpreter_run" description="Executes python 3.12 code for Data Analysis tasks in a docker container. The process output is returned. Do not generate visualizations. The only packages available are numpy===2.1.3, pandas===2.2.3, scipy===1.14.1, matplotlib===3.9.2. There is NO network connectivity. Do not attempt to install other packages or make web requests. You must copy all the necessary files or pass all the data because the python code runs in a separate container." href="/genaiscript/reference/scripts/system#systempython_code_interpreter" />
4242
<LinkCard title="python_code_interpreter_copy_files_to_container" description="Copy files from the workspace file system to the container file system. NO absolute paths. Returns the path of each file copied in the python container." href="/genaiscript/reference/scripts/system#systempython_code_interpreter" />
4343
<LinkCard title="python_code_interpreter_read_file" description="Reads a file from the container file system. No absolute paths." href="/genaiscript/reference/scripts/system#systempython_code_interpreter" />
44-
<LinkCard title="resource_read" description="Read the content of a resource from a URL. Resolves various protocols and returns the content of the files found at the URL." href="/genaiscript/reference/scripts/system#systemresource_read" />
44+
<LinkCard title="resource_list" description="List available resources from the host. Returns a list of available resource URIs and their descriptions." href="/genaiscript/reference/scripts/system#systemresources" />
45+
<LinkCard title="resource_read" description="Read the content of a resource from a URL. Resolves various protocols and returns the content of the files found at the URL." href="/genaiscript/reference/scripts/system#systemresources" />
4546
<LinkCard title="retrieval_fuzz_search" description="Search for keywords using the full text of files and a fuzzy distance." href="/genaiscript/reference/scripts/system#systemretrieval_fuzz_search" />
4647
<LinkCard title="retrieval_vector_search" description="Search files using embeddings and similarity distance." href="/genaiscript/reference/scripts/system#systemretrieval_vector_search" />
4748
<LinkCard title="retrieval_web_search" description="Search the web for a user query using Tavily or Bing Search." href="/genaiscript/reference/scripts/system#systemretrieval_web_search" />

docs/src/content/docs/reference/scripts/system.mdx

Lines changed: 113 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,91 +3971,139 @@ export default function (ctx: ChatGenerationContext) {
39713971
`````
39723972
39733973
3974-
### `system.resource_read`
3974+
### `system.resources`
39753975
39763976
Read resource content from a URL using MCP resource resolution
39773977
39783978
Provides a tool that can read and return the content of resources from URLs using the host's resolveResource function. Supports various protocols including https, file, git, gist, and vscode.
39793979
3980+
- tool `resource_list`: List available resources from the host. Returns a list of available resource URIs and their descriptions.
39803981
- tool `resource_read`: Read the content of a resource from a URL. Resolves various protocols and returns the content of the files found at the URL.
39813982
3982-
`````js wrap title="system.resource_read"
3983+
`````js wrap title="system.resources"
39833984
system({
3984-
title: "Read resource content from a URL using MCP resource resolution",
3985-
description: "Provides a tool that can read and return the content of resources from URLs using the host's resolveResource function. Supports various protocols including https, file, git, gist, and vscode.",
3986-
})
3985+
title: "Read resource content from a URL using MCP resource resolution",
3986+
description:
3987+
"Provides a tool that can read and return the content of resources from URLs using the host's resolveResource function. Supports various protocols including https, file, git, gist, and vscode.",
3988+
});
39873989
39883990
export default function (ctx: ChatGenerationContext) {
3989-
const { defTool } = ctx
3991+
const { defTool } = ctx;
39903992
3991-
const dbg = host.logger("genaiscript:res:read")
3993+
const dbg = host.logger("genaiscript:resources");
39923994
3993-
defTool(
3994-
"resource_read",
3995-
"Read the content of a resource from a URL. Resolves various protocols and returns the content of the files found at the URL.",
3996-
{
3997-
type: "object",
3998-
properties: {
3999-
url: {
4000-
type: "string",
4001-
description: "The URL to read the resource content from. Supports various protocols including https, file, git, gist, and vscode.",
4002-
},
4003-
},
4004-
required: ["url"],
3995+
defTool(
3996+
"resource_list",
3997+
"List available resources from the host. Returns a list of available resource URIs and their descriptions.",
3998+
{
3999+
type: "object",
4000+
properties: {},
4001+
},
4002+
async (args) => {
4003+
const { context } = args;
4004+
4005+
dbg(`listing available resources`);
4006+
4007+
try {
4008+
const resources = await host.resources();
4009+
4010+
if (!resources || resources.length === 0) {
4011+
return "No resources available from host. You can still use builtin protocols like https://, file://, git://, gist:// with the resource_read tool.";
4012+
}
4013+
4014+
dbg(`found ${resources.length} resources`);
4015+
4016+
const results = resources
4017+
.map((resource) => {
4018+
const { uri, name, description, mimeType } = resource;
4019+
let result = `uri: ${uri}`;
4020+
if (name) result += `\nname: ${name}`;
4021+
if (description) result += `\ndescription: ${description}`;
4022+
if (mimeType) result += `\nmime: ${mimeType}`;
4023+
return result;
4024+
})
4025+
.join("\n\n");
4026+
4027+
context.log(`Found ${resources.length} resource(s)`);
4028+
return results;
4029+
} catch (error) {
4030+
const errorMsg = error instanceof Error ? error.message : String(error);
4031+
dbg(`error listing resources: ${errorMsg}`);
4032+
context.log(`Error listing resources: ${errorMsg}`);
4033+
return `Error listing resources: ${errorMsg}`;
4034+
}
4035+
},
4036+
);
4037+
4038+
defTool(
4039+
"resource_read",
4040+
"Read the content of a resource from a URL. Resolves various protocols and returns the content of the files found at the URL.",
4041+
{
4042+
type: "object",
4043+
properties: {
4044+
url: {
4045+
type: "string",
4046+
description:
4047+
"The URL to read the resource content from. Supports MCP resource resolution and various protocols including https, file, git, gist, and vscode.",
40054048
},
4006-
async (args) => {
4007-
const { context, url } = args
4008-
4009-
if (!url) {
4010-
return "Error: URL is required"
4011-
}
4049+
},
4050+
required: ["url"],
4051+
},
4052+
async (args) => {
4053+
const { context, url } = args;
40124054
4013-
dbg(`reading resource from URL: ${url}`)
4014-
context.log(`Reading resource content from: ${url}`)
4055+
if (!url) {
4056+
return "Error: URL is required";
4057+
}
40154058
4016-
try {
4017-
const resource = await host.resolveResource(url)
4018-
4019-
if (!resource) {
4020-
dbg(`failed to resolve resource: ${url}`)
4021-
return `Error: Unable to resolve resource from URL: ${url}`
4022-
}
4059+
dbg(`reading resource from URL: ${url}`);
4060+
context.log(`Reading resource content from: ${url}`);
40234061
4024-
const { uri, files } = resource
4025-
dbg(`resolved ${files.length} files from ${uri.href}`)
4062+
try {
4063+
const resource = await host.resolveResource(url);
40264064
4027-
if (!files || files.length === 0) {
4028-
return `Error: No files found at URL: ${url}`
4029-
}
4065+
if (!resource) {
4066+
dbg(`failed to resolve resource: ${url}`);
4067+
return `Error: Unable to resolve resource from URL: ${url}`;
4068+
}
40304069
4031-
// Return content of all files found
4032-
const results = files.map((file) => {
4033-
if (!file.content) {
4034-
return `File: ${file.filename} (no content available)`
4035-
}
4036-
4037-
const header = `File: ${file.filename}${file.type ? ` (${file.type})` : ""}`
4038-
const separator = "```"
4039-
4040-
if (file.encoding === "base64") {
4041-
return `${header}\n${separator}\n[Base64 encoded content - ${file.content.length} characters]\n${separator}`
4042-
}
4043-
4044-
return `${header}\n${separator}\n${file.content}\n${separator}`
4045-
}).join("\n\n")
4046-
4047-
context.log(`Successfully read ${files.length} file(s) from resource`)
4048-
return results
4049-
4050-
} catch (error) {
4051-
const errorMsg = error instanceof Error ? error.message : String(error)
4052-
dbg(`error reading resource: ${errorMsg}`)
4053-
context.log(`Error reading resource: ${errorMsg}`)
4054-
return `Error reading resource from ${url}: ${errorMsg}`
4055-
}
4070+
const { uri, files } = resource;
4071+
dbg(`resolved ${files.length} files from ${uri.href}`);
4072+
4073+
if (!files || files.length === 0) {
4074+
return `Error: No files found at URL: ${url}`;
40564075
}
4057-
)
4076+
4077+
// Return content of all files found
4078+
const results = files
4079+
.map((file) => {
4080+
if (!file.content) {
4081+
return `File: ${file.filename} (no content available)`;
4082+
}
4083+
4084+
const header = `File: ${file.filename}${file.type ? ` (${file.type})` : ""}`;
4085+
const separator = "```";
4086+
4087+
if (file.encoding === "base64") {
4088+
return `${header}\n${separator}\n[Base64 encoded content - ${file.content.length} characters]\n${separator}`;
4089+
}
4090+
4091+
return `${header}\n${separator}\n${file.content}\n${separator}`;
4092+
})
4093+
.join("\n\n");
4094+
4095+
context.log(`Successfully read ${files.length} file(s) from resource`);
4096+
return results;
4097+
} catch (error) {
4098+
const errorMsg = error instanceof Error ? error.message : String(error);
4099+
dbg(`error reading resource: ${errorMsg}`);
4100+
context.log(`Error reading resource: ${errorMsg}`);
4101+
return `Error reading resource from ${url}: ${errorMsg}`;
4102+
}
4103+
},
4104+
);
40584105
}
4106+
40594107
`````
40604108
40614109

packages/core/genaisrc/system.resource_read.genai.mts

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)