Update resources.mdx

This commit is contained in:
Justin Spahr-Summers 2024-11-21 15:19:27 +00:00
commit 69212f30b0
No known key found for this signature in database

View file

@ -24,7 +24,7 @@ Each resource is identified by a unique URI and can contain either text or binar
Resources are identified using URIs that follow this format: Resources are identified using URIs that follow this format:
``` ```
[protocol]://[path] [protocol]://[host]/[path]
``` ```
For example: For example:
@ -42,7 +42,7 @@ Resources can contain two types of content:
Text resources contain UTF-8 encoded text data. These are suitable for: Text resources contain UTF-8 encoded text data. These are suitable for:
- Source code - Source code
- Configuration files - Configuration files
- Log files - Log files
- JSON/XML data - JSON/XML data
- Plain text - Plain text
@ -66,37 +66,39 @@ Servers expose a list of concrete resources via the `resources/list` endpoint. E
```typescript ```typescript
{ {
uri: string; // Unique identifier for the resource uri: string; // Unique identifier for the resource
name: string; // Human-readable name name: string; // Human-readable name
description?: string; // Optional description description?: string; // Optional description
mimeType?: string; // Optional MIME type mimeType?: string; // Optional MIME type
} }
``` ```
### Resource templates ### Resource templates
For dynamic resources, servers can expose URI templates that clients can use to construct valid resource URIs: For dynamic resources, servers can expose [URI templates](https://datatracker.ietf.org/doc/html/rfc6570) that clients can use to construct valid resource URIs:
```typescript ```typescript
{ {
uriTemplate: string; // URI template following RFC 6570 uriTemplate: string; // URI template following RFC 6570
name: string; // Human-readable name for this type name: string; // Human-readable name for this type
description?: string; // Optional description description?: string; // Optional description
mimeType?: string; // Optional MIME type for all matching resources mimeType?: string; // Optional MIME type for all matching resources
} }
``` ```
## Reading resources ## Reading resources
To read a resource, clients make a `resources/read` request with the resource URI. The server responds with: To read a resource, clients make a `resources/read` request with the resource URI.
The server responds with a list of resource contents:
```typescript ```typescript
{ {
contents: [ contents: [
{ {
uri: string; // The requested URI uri: string; // The URI of the resource
mimeType?: string; // Optional MIME type mimeType?: string; // Optional MIME type
// One of: // One of:
text?: string; // For text resources text?: string; // For text resources
blob?: string; // For binary resources (base64 encoded) blob?: string; // For binary resources (base64 encoded)
@ -105,6 +107,10 @@ To read a resource, clients make a `resources/read` request with the resource UR
} }
``` ```
<Tip>
Servers may return multiple resources in response to one `resources/read` request. This could be used, for example, to return a list of files inside a directory when the directory is read.
</Tip>
## Resource updates ## Resource updates
MCP supports real-time updates for resources through two mechanisms: MCP supports real-time updates for resources through two mechanisms:
@ -113,7 +119,7 @@ MCP supports real-time updates for resources through two mechanisms:
Servers can notify clients when their list of available resources changes via the `notifications/resources/list_changed` notification. Servers can notify clients when their list of available resources changes via the `notifications/resources/list_changed` notification.
### Content changes ### Content changes
Clients can subscribe to updates for specific resources: Clients can subscribe to updates for specific resources:
@ -126,45 +132,58 @@ Clients can subscribe to updates for specific resources:
Here's a simple example of implementing resource support in an MCP server: Here's a simple example of implementing resource support in an MCP server:
```typescript <Tabs>
const server = new Server({ <Tab title="TypeScript">
name: "example-server", ```typescript
version: "1.0.0" const server = new Server({
}); name: "example-server",
version: "1.0.0"
// List available resources }, {
server.setRequestHandler(ListResourcesRequestSchema, async () => { capabilities: {
return { resources: {}
resources: [
{
uri: "file:///logs/app.log",
name: "Application Logs",
mimeType: "text/plain"
} }
] });
};
});
// Read resource contents // List available resources
server.setRequestHandler(ReadResourceRequestSchema, async (request) => { server.setRequestHandler(ListResourcesRequestSchema, async () => {
const uri = request.params.uri; return {
resources: [
if (uri === "file:///logs/app.log") { {
const logContents = await readLogFile(); uri: "file:///logs/app.log",
return { name: "Application Logs",
contents: [ mimeType: "text/plain"
{ }
uri, ]
mimeType: "text/plain", };
text: logContents });
}
] // Read resource contents
}; server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
} const uri = request.params.uri;
throw new Error("Resource not found"); if (uri === "file:///logs/app.log") {
}); const logContents = await readLogFile();
``` return {
contents: [
{
uri,
mimeType: "text/plain",
text: logContents
}
]
};
}
throw new Error("Resource not found");
});
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Best practices ## Best practices
@ -194,4 +213,4 @@ When exposing resources:
- Encrypt sensitive data in transit - Encrypt sensitive data in transit
- Validate MIME types - Validate MIME types
- Implement timeouts for long-running reads - Implement timeouts for long-running reads
- Handle resource cleanup appropriately - Handle resource cleanup appropriately