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:
```
[protocol]://[path]
[protocol]://[host]/[path]
```
For example:
@ -75,7 +75,7 @@ Servers expose a list of concrete resources via the `resources/list` endpoint. E
### 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
{
@ -88,13 +88,15 @@ For dynamic resources, servers can expose URI templates that clients can use to
## 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
{
contents: [
{
uri: string; // The requested URI
uri: string; // The URI of the resource
mimeType?: string; // Optional MIME type
// One of:
@ -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
MCP supports real-time updates for resources through two mechanisms:
@ -126,14 +132,20 @@ Clients can subscribe to updates for specific resources:
Here's a simple example of implementing resource support in an MCP server:
```typescript
const server = new Server({
<Tabs>
<Tab title="TypeScript">
```typescript
const server = new Server({
name: "example-server",
version: "1.0.0"
});
}, {
capabilities: {
resources: {}
}
});
// List available resources
server.setRequestHandler(ListResourcesRequestSchema, async () => {
// List available resources
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: [
{
@ -143,10 +155,10 @@ server.setRequestHandler(ListResourcesRequestSchema, async () => {
}
]
};
});
});
// Read resource contents
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
// Read resource contents
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const uri = request.params.uri;
if (uri === "file:///logs/app.log") {
@ -163,8 +175,15 @@ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
}
throw new Error("Resource not found");
});
```
});
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Best practices