Skip to content

feat: add Resources and Prompts primitive examples#16

Merged
heznpc merged 1 commit intomainfrom
feat/resources-prompts-primitives
Apr 23, 2026
Merged

feat: add Resources and Prompts primitive examples#16
heznpc merged 1 commit intomainfrom
feat/resources-prompts-primitives

Conversation

@heznpc
Copy link
Copy Markdown
Member

@heznpc heznpc commented Apr 23, 2026

Summary

Brings MCP primitive coverage up to parity with the TS sibling (mcp-server-starter). The starter now demonstrates all three MCP primitives out of the box:

  • Tool — existing greet (unchanged)
  • Resource — new server-info at info://server/status
  • Prompt — new code-review with pydantic-validated args

Changes

Resources (src/my_mcp_server/resources/server_info.py)

  • FastMCP API: @mcp.resource(uri, *, name, title, description, mime_type) decorator, handler returns str (JSON) — FastMCP takes care of wrapping into the MCP contents envelope.
  • Reads package name + version from pyproject.toml via tomllib (walks up from the source file), with an importlib.metadata fallback for wheel installs where the source tree isn't shipped.
  • Runtime block includes Python version (sys.version), platform (platform.system()), and arch (platform.machine()).

Prompts (src/my_mcp_server/prompts/code_review.py)

  • FastMCP API: @mcp.prompt(name, title, description) wrapping a function whose signature defines the argsSchema; return type is list[UserMessage].
  • Args validated by pydantic @validate_call: language: Literal["py","js","ts","go"] and code: Annotated[str, Field(min_length=1)]. Empty code and unsupported languages raise ValidationError before the handler body runs.
  • Returns a single user message with a language-labeled fenced code block (e.g. ```py).

Tests (14 total, all green locally)

  • tests/test_server_info.py — identity metadata, JSON shape, version matches pyproject.toml, Python runtime matches sys.version, resource is registered on the server.
  • tests/test_code_review.py — identity metadata, rejects empty code / unsupported language / missing fields, happy-path message shape, per-enum language label interpolation, prompt is registered on the server.

Test audit (Task D)

  • Mutation-checked test_greet by changing the handler's return to "Greetings, ". Both existing test_tools.py tests failed as expected — these are real behavioral assertions, not tautologies or file-existence checks. Reverted.
  • No bogus tests found. pyproject.toml already has testpaths = ["tests"] with asyncio_mode = "auto" and does not use --passWithNoTests-style masking.

README

  • English + Korean READMEs gain Adding Resources / Adding Prompts sections (English) and the Korean equivalents, plus Resources/Prompts bullets under "What You Get". The project tree diagram in README.md now shows resources/ and prompts/ modules and the two new test files.

FastMCP API shape used

# Resource
mcp.resource(
    "info://server/status",
    name="server-info",
    title="Server Info",
    description="...",
    mime_type="application/json",
)(async_handler_returning_str)

# Prompt
mcp.prompt(
    name="code-review",
    title="Code Review",
    description="...",
)(validate_call_decorated_handler_returning_list_UserMessage)

Test plan

  • pytest -v — 14 passed
  • ruff check . — all checks passed
  • ruff format --check . — formatted
  • mypy src/ — no issues (9 source files)
  • End-to-end smoke: mcp.list_tools() / list_resources() / list_prompts() / read_resource() / get_prompt() all return the expected shapes
  • CI green on 3.11 / 3.12 / 3.13

Brings MCP primitive coverage up to parity with the TS sibling
(mcp-server-starter): adds a server-info Resource and a code-review
Prompt alongside the existing greet Tool, so the starter now
demonstrates all three MCP primitives out of the box.

- resources/server_info.py exposes info://server/status as JSON with
  name/version (read from pyproject.toml via tomllib) plus Python +
  platform runtime info.
- prompts/code_review.py uses pydantic @validate_call to reject empty
  code and unsupported languages before the handler runs, rendering a
  single user message with a language-labeled fenced code block.
- tests cover identity metadata, registration on the server, handler
  shape, and pydantic validation failures (14 tests total, all green).
- README.md and README.ko.md gain Resources/Prompts sections and the
  tree diagram now reflects the new modules.
@heznpc heznpc merged commit bb98e6c into main Apr 23, 2026
7 checks passed
@heznpc heznpc deleted the feat/resources-prompts-primitives branch April 23, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant