0Pricing
MCP Academy · 课时

提供静态文本资源

公开一份固定文档供模型读取。

提供静态文本资源 是 CoddyKit 上的免费 MCP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MCP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MCP Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

A Fixed Document

The simplest resource is a static one: a fixed string your server returns every time. Great for help text, policies, or config notes.

One Function, One URI

You expose it by writing a Python function and tagging it with a resource URI. When a client reads that URI, your function runs and returns text.

The Decorator

The resource decorator registers the function under its URI. The string you pass becomes the address clients use to fetch the content.

@mcp.resource("info://welcome")
def welcome():
    return "Welcome to the server!"

Return Plain Text

For a text resource, just return a Python string. The SDK wraps it into the proper MCP read response so the client receives clean content.

Name and Describe It

Give the resource a clear description so the model knows when to read it. A docstring on the function is used as that description.

@mcp.resource("info://welcome")
def welcome():
    "Greeting shown to new users."
    return "Hello there!"

Static Means No Inputs

A static resource takes no parameters; it always yields the same body. If you need to vary output, that is a job for templates, covered later.

Build Text in Code

The return value can be assembled in Python before sending. The result is still static to the client, since the same call gives the same text.

@mcp.resource("config://limits")
def limits():
    return "max_items=100\ntimeout=30"

Why Resources, Not Tools

Use a resource when the model only needs to read context. Tools are for actions; reading reference text is exactly what resources are for.

The Model Pulls It In

Many hosts let the user attach a resource to a chat, dropping its content into context. The model then reasons over that text alongside the prompt.

Keep It Lightweight

Static resources load fast since there is no work to do. Reserve them for small, stable text so you do not flood the model with tokens.

Test Your Resource

Run the server and use a client to list and read the URI. Seeing your exact string come back confirms the resource is wired correctly.

Quick Check

Choosing the right primitive for fixed text.

Recap

A static resource is a function under a URI that returns fixed text. Add a docstring description and keep the body small. 📄

常见问题解答

「提供静态文本资源」课时是免费的吗?

是的 — 「提供静态文本资源」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MCP Academy 课程的其余内容,请升级到 CoddyKit PRO。 MCP Academy 课程共包含 4 节课。

「提供静态文本资源」这节课中我会学到什么?

公开一份固定文档供模型读取。 你通过在浏览器中直接运行的动手代码来练习 MCP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MCP Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MCP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「提供静态文本资源」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MCP Academy 课中编写并运行代码吗?

能。每节 MCP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 解释资源 URI
  2. 提供静态文本资源
  3. 从磁盘读取文件
  4. 设置正确的 MIME 类型
← 返回 MCP Academy