伊春市网站建设_网站建设公司_前后端分离_seo优化
2026/1/17 19:34:48 网站建设 项目流程
In previous exercises, you learned how to extend your MCP server with tools—server-defined functions that let clients perform actions or retrieve data. But sometimes, what you want to expose isn't just a function, but a resource: a file, a database record, or some other piece of structured data that can be read (and sometimes listed or searched) by clients.
The Model Context Protocol (MCP) has the concept of resources to make this possible. Resources are a standardized way for servers to share contextual data with clients—such as files, database entries, or application metadata—using a uniform interface. Each resource is uniquely identified by a URI and can be described with metadata like a name, description, and MIME type.
From the MCP Spec: Resources in MCP are designed to be application-driven, with host applications determining how to incorporate context based on their needs. However, implementations are free to expose resources through any interface pattern that suits their needs—the protocol itself does not mandate any specific user interaction model.
 
Example request to read a resource:
{"jsonrpc": "2.0","id": 2,"method": "resources/read","params": {"uri": "taco://menu/items/carne-asada"}
}
Example response:
{"jsonrpc": "2.0","id": 2,"result": {"contents": [{"uri": "taco://menu/items/carne-asada","mimeType": "application/json","text": "{\"name\":\"Carne Asada Taco\",\"ingredients\":[\"steak\",\"tortilla\",\"onion\",\"cilantro\"],\"instructions\":\"Grill the steak, chop into small pieces, serve on warm tortillas\"}"}]}
}

 

ResourceTemplate: you can assign each resoure a uri to load a single resource.

Resource templates let you define parameterized resources, like epicme://entries/{id} or epicme://tags/{id}. This means clients can discover and read individual entries or tags by their unique identifiers, just like accessing a file by its path.

import { invariant } from '@epic-web/invariant'
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
import { type EpicMeMCP } from './index.ts'export async function initializeResources(agent: EpicMeMCP) {agent.server.registerResource('tags','epicme://tags',{title: 'Tags',description: 'All tags currently in the database',},async (uri) => {const tags = await agent.db.getTags()return {contents: [{mimeType: 'application/json',text: JSON.stringify(tags),uri: uri.toString(),},],}},)agent.server.registerResource('tag',new ResourceTemplate('epicme://tags/{id}', {// 🐨 implement this list callback to get all tags (💰 agent.db.getTags())// 🐨 return an object with a "resources" property that is an array of resource listings (💰 each object has a name, uri, and mimeType).complete: {async id(value) {const tags = await agent.db.getTags()return tags.map((tag) => tag.id.toString()).filter((id) => id.includes(value)).slice(0, 100)},},list: async () => {const tags = await agent.db.getTags()return {resources: tags.map((tag) => ({name: tag.name,uri: `epicme://tags/${tag.id}`,mimeType: 'application/json',})),}},}),{title: 'Tag',description: 'A single tag with the given ID',},async (uri, { id }) => {const tag = await agent.db.getTag(Number(id))invariant(tag, `Tag with ID "${id}" not found`)return {contents: [{mimeType: 'application/json',text: JSON.stringify(tag),uri: uri.toString(),},],}},)agent.server.registerResource('entry',new ResourceTemplate('epicme://entries/{id}', {complete: {async id(value) {const entries = await agent.db.getEntries()return entries.map((entry) => entry.id.toString()).filter((id) => id.includes(value)).slice(0, 100)},},list: undefined,}),{title: 'Journal Entry',description: 'A single journal entry with the given ID',},async (uri, { id }) => {const entry = await agent.db.getEntry(Number(id))invariant(entry, `Entry with ID "${id}" not found`)return {contents: [{mimeType: 'application/json',text: JSON.stringify(entry),uri: uri.toString(),},],}},)
}

 

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询