Update resources.mdx
This commit is contained in:
parent
a2e6868c40
commit
69212f30b0
1 changed files with 70 additions and 51 deletions
|
|
@ -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:
|
||||||
|
|
@ -66,35 +66,37 @@ 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:
|
||||||
|
|
@ -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:
|
||||||
|
|
@ -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: [
|
||||||
|
{
|
||||||
|
uri: "file:///logs/app.log",
|
||||||
|
name: "Application Logs",
|
||||||
|
mimeType: "text/plain"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
if (uri === "file:///logs/app.log") {
|
// Read resource contents
|
||||||
const logContents = await readLogFile();
|
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
||||||
return {
|
const uri = request.params.uri;
|
||||||
contents: [
|
|
||||||
{
|
|
||||||
uri,
|
|
||||||
mimeType: "text/plain",
|
|
||||||
text: logContents
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue