C.W.K.
Stream
Lesson 02 of 05 · published

Resources — Read-Only 데이터

~22 min · resources, uri, read, subscribe

Level 0호기심 많은 독자
0 XP0/48 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Resources 는 MCP 의 read-only 쪽. 안정된 identity 가지고 fetch 가능한 거 다 모델: 디스크 파일, DB row, docs 페이지, memory state snapshot. 각 resource 는 URI (wire 위 이름), mime type, optional annotation, text 또는 binary 인 content.

Protocol 이 resource method 셋 노출: resources/list 가 사용 가능 resource 반환, resources/read 가 URI 로 한 resource content 반환, resources/subscribe (server 가 광고할 때) 는 client 가 특정 resource 변경 알림 받게. Subscriber 는 host 가 panel 을 up-to-date 유지하고 싶을 때 live-data UI 의 토대.

맞는 mental model: Resources 는 user 한테 안 묻고 host 가 읽을 수 있는 거. 디자인상 안전 — side effect 없음, state 변이 없음, 되돌릴 수 없는 동작 없음. 그래서 host 가 confirmation 요구 없이 공격적으로 list 하고 pre-fetch 가능.

흔한 패턴: Postgres MCP server 가 table 당 resource 하나 노출 (URI postgres:///orders), filesystem server 가 허용된 path 당 resource, docs server 가 page 당 resource. Tool 은 변이; Resource 는 그냥 read 되길 기다리며 앉아있어.

Code

Python SDK 에서 resource 정의·python
from mcp.server.fastmcp import FastMCP

app = FastMCP("docs-server")

@app.resource("docs://README")
def read_readme() -> str:
    return open("/srv/docs/README.md").read()

@app.resource("docs://changelog/{version}")
def read_changelog(version: str) -> str:
    return open(f"/srv/docs/changelog/{version}.md").read()
Client 쪽에서 resource read·python
async with ClientSession(read, write) as s:
    await s.initialize()
    listing = await s.list_resources()
    for r in listing.resources:
        print(r.uri, r.name, r.mimeType)
    content = await s.read_resource("docs://README")
    print(content.contents[0].text)

External links

Exercise

FastMCP server 작게 짜고 resource 둘 노출: 정적 (고정 README) 와 templated (echo://{message}). Client SDK 스크립트로 연결, list, 둘 다 read. List/read 대칭은 모든 server 짤 때 동일.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.