본문 바로가기
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 의 읽기 전용 쪽이야. 고유한 이름을 갖고 가져올 수 있는 건 뭐든 여기로 들어와. 디스크의 파일, DB 의 row, 문서 페이지, 메모리 상태의 스냅샷. Resource 하나에는 URI (선 위에서 쓰는 이름), mime type, 붙여도 되는 annotation, 그리고 텍스트나 바이너리인 content 가 있어.

Protocol 이 열어둔 resource method 는 셋이야. resources/list 는 쓸 수 있는 resource 를 돌려주고, resources/read 는 URI 로 지목한 resource 하나의 content 를 돌려줘. resources/subscribe 는 server 가 지원한다고 밝힌 경우에만 쓸 수 있는데, 특정 resource 가 바뀌면 client 가 알림을 받게 해줘. 이 구독이 host 가 화면을 늘 최신으로 유지하고 싶을 때 실시간 UI 의 바탕이 돼.

머릿속에 넣어둘 그림은 이거야. Resource 는 user 한테 묻지 않고 host 가 읽어도 되는 것. 설계상 안전하거든 — side effect 도 없고, 상태를 바꾸지도 않고, 되돌릴 수 없는 동작도 없어. 그래서 host 가 확인 절차 없이 마음껏 목록을 뽑고 미리 당겨올 수 있는 거야.

흔한 모양은 이래. Postgres MCP server 는 table 마다 resource 를 하나씩 열어두고 (URI 는 postgres:///orders 같은 식), filesystem server 는 허용된 경로마다, 문서 server 는 페이지마다 하나씩 열어. Tool 이 뭔가를 바꾸는 쪽이라면, Resource 는 그냥 읽히기를 기다리며 가만히 앉아 있는 쪽이야.

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 하나와 템플릿을 쓰는 echo://{message} 하나. Client SDK 스크립트로 붙어서 목록을 뽑고 둘 다 읽어봐. 목록과 읽기가 짝을 이루는 이 모양은 어떤 server 를 짜든 똑같아.

Progress

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

댓글 0

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

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