Skip to content

Comments

Import "Build an LLM-powered chatbot" quickstart guide#2130

Draft
jonathanhefner wants to merge 9 commits intomodelcontextprotocol:mainfrom
jonathanhefner:client-quickstart
Draft

Import "Build an LLM-powered chatbot" quickstart guide#2130
jonathanhefner wants to merge 9 commits intomodelcontextprotocol:mainfrom
jonathanhefner:client-quickstart

Conversation

@jonathanhefner
Copy link
Member

ℹ️ This is based on top of #2119.


Import and adapt the client quickstart tutorial from modelcontextprotocol.io and the companion mcp-client-python example from modelcontextprotocol/quickstart-resources into this repo.

docs/client-quickstart.md uses <!-- snippet-source --> comments so code snippets are type-checked and kept in sync with the full runnable example in examples/clients/quickstart-client/.

The new script is a superset of the old one. It continues to handle the
existing `<!-- snippet-source -->` markers in `README.v2.md`, and adds
support for syncing code snippets into Python docstrings and markdown
docs under `docs/`.

New capabilities beyond the old script:

- Region extraction from example files using `# region` / `# endregion`
  markers, so a single example file can provide multiple snippets
- All source paths resolve relative to the repository root
- Scans `src/**/*.py` and `docs/**/*.md` in addition to the README
- Caches file contents and extracted regions for efficiency

The marker format uses HTML comments (`<!-- snippet-source -->` /
`<!-- /snippet-source -->`), which are invisible when rendered by
`mkdocstrings` and do not interfere with `pymdownx.superfences` code
fence parsing.
Add support for `<!-- snippet-source #region_name -->` markers that omit
the companion file path. The path is derived from the target file's
location using the mapping `src/X` → `examples/snippets/docstrings/X`.

This eliminates line-length violations on marker lines in docstrings,
since the full companion path no longer needs to be embedded in every
marker. The full-path form continues to work for non-`src/` targets like
markdown files.
Move inline code examples from docstrings in `src/mcp/` into standalone
companion files at `examples/snippets/docstrings/mcp/`, mirroring the
source tree structure. The `scripts/sync_snippets.py` script keeps the
docstring content in sync with the companion files via
`<!-- snippet-source #RegionName -->` markers.

This ensures all docstring examples are checked by pyright and ruff,
catching type errors and style drift that would otherwise go unnoticed
in raw docstring text. The pattern follows the TypeScript SDK's approach
of one companion file per source file, with each example in a named
function whose parameters supply typed context.

19 companion files cover 42 code examples across the public API surface:
`MCPServer`, `Client`, `ClientSession`, `Context`, `ResponseRouter`,
`ServerTaskContext`, `ExperimentalTaskHandlers`,
`ClientCredentialsOAuthProvider`, `PrivateKeyJWTOAuthProvider`,
`SignedJWTParameters`, and others.

All source markers use path-less form (`#RegionName`) — the companion
path is derived automatically from the target file location. Region
names follow `ClassName_methodName_variant` without abbreviation.

Pyright execution environment for the companion files suppresses only
"unused artifact" diagnostics inherent to example code
(`reportUnusedFunction`, `reportUnusedVariable`, `reportAbstractUsage`,
`reportUnusedClass`, `reportPrivateUsage`). All actual type-checking
rules remain enabled.
Add a "Docstring Code Examples" section covering the companion file
system: directory layout, `<!-- snippet-source #RegionName -->` markers,
`ClassName_methodName_variant` naming, the function-parameter pattern
for typed dependencies, and the prohibition on type-suppression comments
inside regions.
`sync_snippets.py` also syncs snippet-source markers in `docs/**/*.md`
and `README.v2.md`, not just `src/` docstrings. Add a short section
after "Docstring Code Examples" noting that these files use explicit
paths (path-less `#Region` markers are only supported in `src/` files).
The previous documentation had two flat sections where the docstring
section contained all the detail and the markdown section referred to it
vaguely. Reorganize into a `## Code Snippet System` intro covering
shared concepts (marker format, region extraction, naming conventions,
function wrappers, typed params, `# type: ignore` prohibition, pyright
workflow) with `### Markdown Code Examples` and
`### Docstring Code Examples` subsections covering only what is unique
to each target type.
The links to snippet source files are redundant since the code blocks
already contain the entire source. Removes all 33 occurrences.
Import the quickstart MCP client from
`modelcontextprotocol/quickstart-resources` into
`examples/clients/quickstart-client/`. This is the companion code for
the "Building MCP clients" tutorial at modelcontextprotocol.io.

The client connects to any MCP server via stdio, discovers its tools,
and runs an interactive chat loop where user queries are sent to Claude
with the server's tools available for use.

Changes from the upstream source:

- Moved `import sys` to top-level imports
- Added return type annotations (`-> None`) to all methods
- Added `assert self.session is not None` guards for type narrowing
- Changed `tool.inputSchema` to `tool.input_schema` (SDK Python name)
- Added proper Anthropic type annotations (`MessageParam`, `ToolParam`,
  `TextBlock`, `ToolUseBlock`, `ToolResultBlockParam`, `TextBlockParam`)
- Fixed tool result handling to use proper `ToolResultBlockParam` with
  `tool_use_id` linkage and explicit MCP `TextContent` to Anthropic
  `TextBlockParam` conversion, replacing the original code which passed
  raw MCP content objects to the Anthropic API

Root `pyproject.toml` adds `mcp-quickstart-client` as a dev dependency
with a workspace source mapping. This is necessary because `anthropic`
is a new external dependency not already in the root package's
transitive dependency tree, and without this, `uv sync --all-extras`
(used by CI) would not install it, causing `pyright` to fail.
Convert the "Building MCP clients" tutorial from modelcontextprotocol.io
(Mintlify MDX) into a Diataxis-style tutorial for the Python SDK docs.
Code blocks are synced from
`examples/clients/quickstart-client/client.py` via
`<!-- snippet-source -->` markers and `# region` tags, so the tutorial
stays in sync with the actual example code that gets type-checked.

Changes to the tutorial content (vs the original Mintlify source):

- Strip all Mintlify JSX (`<Tabs>`, `<CodeGroup>`, `<Warning>`, etc.)
  and convert to MkDocs Material syntax (`===` tabs, `!!! admonitions`)
- Remove non-tutorial sections per Diataxis: "Key Components Explained",
  "Common Customization Points", "Best Practices"
- Merge "System Requirements" and API key setup into a single
  "Prerequisites" section; show API key inline in run commands
- Use sentence case for headings throughout
- Tailor troubleshooting errors to real Python exceptions

Changes to the example at `examples/clients/quickstart-client/`:

- Add `# region` / `# endregion` markers for 5 snippet regions
- Remove `python-dotenv` dependency, `load_dotenv()`, and `.env.example`
  (API key is now passed inline via environment variable)
- Update `README.md` link to point to the new tutorial
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