FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Embed code snippets for README from executable examples by ihrpr · Pull Request #1055 · modelcontextprotocol/python-sdk · GitHub

Embed code snippets for README from executable examples - #1055

Merged
ihrpr merged 27 commits into
mainfrom
ihrpr/readme-from-examples
Jul 4, 2025
Merged

Embed code snippets for README from executable examples#1055
ihrpr merged 27 commits into
mainfrom
ihrpr/readme-from-examples

Conversation

ihrpr commented Jun 29, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

We already had an integration test that verifies all current functionality works as expected, so we can use it as both a server example and as the source for README snippets. These snippets will be automatically updated and verified through CI checks. In the future, we'll also add an everything client example and include it in both the README and integration tests.

  1. Automated README Snippets
  • Added scripts/update_readme_snippets.py that extracts code from example files into README
  • Uses <!-- snippet-source path/to/file.py --> markers to identify updatable sections
  • Automatically adds links to full example files on GitHub with line highlighting
  • Supports both full-file and line-range extraction (e.g., #L10-L20)
  1. New Example Structure
  • Created examples/snippets/ with single-feature examples:
    • basic_tool.py - Simple tool example
    • basic_resource.py - Resource handling
    • basic_prompt.py - Prompt creation
    • tool_progress.py - Progress reporting
    • sampling.py - LLM sampling
    • elicitation.py - User interaction
    • completion.py - Argument completion
    • notifications.py - Logging and notifications
  • Each example is self-contained
  1. Everything Server Extraction
  • Moved the comprehensive make_everything_fastmcp() function from tests into a standalone example at examples/servers/everything/
  • Everything server remains available for testing all MCP features in one place
  1. CI Integration
  • Added readme-snippets job to .github/workflows/shared.yml to verify snippets are current
  • Added pre-commit hook that checks snippet freshness on relevant file changes
  • Both use --check mode to fail if snippets are outdated

Comment thread .github/workflows/shared.yml Fixed
Comment thread README.md Outdated

The Context object gives your tools and resources access to MCP capabilities:

<!-- snippet-source examples/servers/everything/src/everything/server.py#L43-L58 -->

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

this is only one example for this PR, when we agree on the process, will replace the rest of the Server examples (and later client) in README as separate PRs

dsp-ant self-requested a review June 30, 2025 09:11
ihrpr changed the title RFC: Everything server as source of truth for integration tests and README snippets Embed code snippets for README from actual executable examples Jul 1, 2025
ihrpr changed the title Embed code snippets for README from actual executable examples Embed code snippets for README from executable examples Jul 1, 2025
ihrpr marked this pull request as ready for review July 2, 2025 10:11

ochafik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Neat!!

severity="high",
title="Heat Wave Warning",
description="Temperatures expected to exceed 40°C",
description="Temperatures expected to exceed 40 degrees",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

nit: revert to celsius to avoid frowns from US users?

Comment thread scripts/update_readme_snippets.py Outdated
# In check mode, only check if code has changed
if check_mode:
# Extract existing code from the match
existing_content = match.group(5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

move up w/ the others?

Comment thread scripts/update_readme_snippets.py Outdated
# Build the replacement block
replacement = f"""{indent}<!-- snippet-source {file_path}{line_ref} -->
{indent}```python
{code}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Might want to indent the code here too {indent}{code.replace('\n', f'\n{indent}')} (markdown should remove it)

Comment thread examples/snippets/servers/__init__.py Outdated
from typing import Literal

# Available snippet modules
SNIPPET_MODULES = [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Given you're not using the #line syntax, i wonder if you could just get the list of snippets on the fly from readme parser, avoid checking in these generated files / evaluate them on the fly (maybe create a temp directory, add it to the PYTHONPATH programmatically and load adhoc $tmp/snippet$i.py modules?)

(would also remove the need for a file name)

Kludex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

As mentioned on Discord, I don't think this will provide a good experience for readers.

We should have a setup in place where every example is an MRE in the documentation.


For inspiration, FastAPI and PydanticAI (popular python projects) have already this kind of setup in their repositories - although their setup is for the MkDocs, not for the README.

ihrpr commented Jul 4, 2025

Copy link
Copy Markdown
Contributor Author

We should have a setup in place where every example is an MRE in the documentation.

@Kludex, the examples are now all standalone files, what am I missing?

ihrpr requested a review from Kludex July 4, 2025 07:52

Kludex left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

4 participants


Back | FazBrowse Home | New Git URL