Skip to main content

Reusable utility methods for MCP servers

Project description

mcp-methods

Shared Rust-powered utilities for MCP servers. Pip-installable library that provides fast file search, GitHub integration, and text processing — the common building blocks needed when writing MCP tool servers.

Install

pip install mcp-methods

For development (requires Rust toolchain + maturin):

pip install -e ".[dev]"

What's included

Function Purpose
list_dir Tree-formatted directory listing with depth control, glob filtering, .gitignore support, dir summaries, and annotation callback
ripgrep_files Ripgrep-powered file search with parallel walking, early termination, context lines, and multiple output modes
ripgrep Drop-in replacement for the Claude Code Grep tool interface
read_file Safe file reading with path traversal protection and line range support
github_discussions Fetch a single issue/PR with smart compaction, or list issues/PRs with filters
git_api GitHub REST API wrapper with token auth
ElementCache Drill-down cache for collapsed elements (code blocks, comments, patches, thread segments) in GitHub discussions
ripgrep_lines Search through text lines with context window merging
ripgrep_json_fields Extract fields from JSON text
compact_discussion / compact_text / collapse_code_blocks Text compaction utilities
extract_github_refs Parse GitHub issue/PR references from text
detect_git_repo / validate_repo Git repository detection and validation

Python API

list_dir(path, *, depth=1, glob=None, dirs_only=False, relative_to=None, respect_gitignore=True, skip_dirs=None, include_size=False, annotate=None)

Tree-formatted directory listing.

from mcp_methods import list_dir

# Basic tree
tree = list_dir("/project/src", depth=2, glob="*.py", relative_to="/project")

# With annotation callback (e.g. loc from knowledge graph)
def get_loc(rel_path):
    node = graph.get_file(rel_path)
    return f"({node.loc} loc)" if node else None

tree = list_dir("/project/src", depth=2, annotate=get_loc)
# src/
# ├── main.py        (144 loc)
# ├── utils.py       (28 loc)
# └── models/
#     ├── user.py    (89 loc)
#     └── post.py    (112 loc)

ripgrep(pattern, *, path=".", glob="*", type=None, output_mode="files_with_matches", max_results=None, offset=0, ...)

Claude Code Grep-compatible interface.

from mcp_methods import ripgrep

results = ripgrep(r"def \w+", path="/project", type="py", max_results=50)

ripgrep_files(source_dirs, pattern, *, glob="*", type_filter=None, output_mode="content", max_results=None, offset=0, match_limit=None, relative_to=None, ...)

Full interface with multi-directory search. max_results limits output entries, match_limit caps the search engine for early termination.

from mcp_methods import ripgrep_files

results = ripgrep_files(
    ["/project"],
    r"def \w+",
    type_filter="py",
    relative_to="/project",
    match_limit=500,
    max_results=100,
)

github_discussions(*, repo=None, number=None, kind="all", state="open", sort="created", limit=20, labels=None)

Fetch a single discussion or list discussions.

from mcp_methods import github_discussions, ElementCache

# List open issues
issues = github_discussions(repo="owner/repo", kind="issue", state="open")

# List pull requests
prs = github_discussions(repo="owner/repo", kind="pr", limit=10)

# Fetch a single issue/PR with smart compaction
issue = github_discussions(repo="owner/repo", number=123)

ElementCache — progressive disclosure for GitHub discussions

Cache for drill-down into collapsed elements. Fetches a discussion once, then lets you explore code blocks, comments, and PR diffs without re-fetching.

from mcp_methods import ElementCache

cache = ElementCache()

# First call fetches from GitHub API, compacts, and caches elements
text = cache.fetch_discussion("owner/repo", 123)

# Subsequent calls return cached summary (no network)
summary = cache.fetch_discussion("owner/repo", 123)
# → "Cached owner/repo#123 — 5 elements available: cb_1, comment_2, patch_1, patch_2, patch_3"

# Force re-fetch when discussion has changed
text = cache.fetch_discussion("owner/repo", 123, refresh=True)

# Drill into a collapsed code block
code = cache.retrieve("owner/repo", 123, "cb_1")

# Drill into a PR patch with grep
result = cache.retrieve("owner/repo", 123, "patch_1", grep="error_handler")

# Drill into a patch with line range
result = cache.retrieve("owner/repo", 123, "patch_2", lines="10-30")

# List available elements
ids = cache.available("owner/repo", 123)

PR diffs are automatically collapsed into patch_N elements in the compact view. Each patch stores the filename, additions/deletions, and full diff text — supporting grep and line-range drill-down.

Large discussions (50+ comments) are automatically digested: first 5 + maintainer highlights + last 5 comments shown inline, with the full middle cached as individual comment_N elements and a searchable comments_middle segment.

git_api(repo, path, *, truncate_at=80000)

GitHub REST API wrapper. For comparing branches/tags, use compare:

from mcp_methods import git_api

# Compare two refs
diff = git_api("owner/repo", "compare/main...feature-branch")

# List commits
commits = git_api("owner/repo", "commits?per_page=10")

read_file(path, allowed_dirs, *, offset=0, limit=0, max_chars=0, transform=None)

Safe file reading with path traversal protection.

from mcp_methods import read_file

content = read_file("src/main.py", ["/project"])

Architecture

All heavy lifting is in Rust (PyO3/maturin), compiled to a native Python extension:

  • grep: Uses grep-regex, grep-searcher, and ignore crates directly (not a ripgrep subprocess). Parallel file walking with per-thread searcher reuse, mmap, SIMD literal optimization, and .gitignore support.
  • GitHub: HTTP via ureq, JSON processing via serde_json, text compaction in Rust. PR diffs are collapsed into cacheable elements for progressive disclosure.
  • File I/O: Path validation and traversal protection in Rust.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mcp_methods-0.3.21-cp313-cp313-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.13Windows x86-64

mcp_methods-0.3.21-cp313-cp313-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.21-cp313-cp313-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mcp_methods-0.3.21-cp312-cp312-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.12Windows x86-64

mcp_methods-0.3.21-cp312-cp312-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.21-cp312-cp312-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mcp_methods-0.3.21-cp311-cp311-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.21-cp311-cp311-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.21-cp311-cp311-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

mcp_methods-0.3.21-cp310-cp310-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.21-cp310-cp310-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.21-cp310-cp310-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file mcp_methods-0.3.21-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c6f12942f157f6d75812744da52a1d19e17040c52ecb840e2138d9ac4c207061
MD5 d3a4d9bd435b6a66c2bf38ad8e64c8c9
BLAKE2b-256 fec483e8b93ca5e9960749bdff23d4995c6f4a7c081e3019514466ccc47061fa

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c366b86043d4c344f2a866dd57568544c06dc35da5a525a67cf439ef8d516aec
MD5 c4be218de15bf6fde0bdf3f435b6e72f
BLAKE2b-256 2bb6c8f528b665527112cae18ae50c3fb4c241f2ea3bd9faef7459b4aaaf4db2

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a8040f5f2d2d4f179ec358603c5b7f71ac63ef39a10b9ae494f4114b17fb38fe
MD5 e2bb7e340081062adab1ecf3a0f3d982
BLAKE2b-256 5b62b39b128f8987a34a935080a756748be446cca73ee99f4abbe1c24a53a8b1

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f7b133dfc03e242b74e2aead161c860bbf1ba4c15a52da5b43be9edf42ffdee2
MD5 3bba79efb3f2f311824222bb3d2f534b
BLAKE2b-256 3a121abb32e787c8408c415950c1adf681384cfc604cf5342a3400cdffb6e18c

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 9144292233179fe28a93138740cde4437518d339fe08af76a9725698e8a835e2
MD5 6912185f13efbd18a83ce62ad1869547
BLAKE2b-256 f99ba058dac199bcab0205e4db58612196b976b6d9c005eece7012427f348d21

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 06032498f1513ad32f356cca9c9804ab21f0054797712fb467135881031ada4b
MD5 156dd0f915b552127cf68e8b4ab602bf
BLAKE2b-256 e5a9bb0e599a854c668d19087bf427a128bb300654511e2fb77dcdef3639ba02

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5b1130f477c00541513fc15ad59e44308a7ecf17616f723d132f1986b2cf7c96
MD5 2c7928348ce592ac56435daa5a5b65e2
BLAKE2b-256 3ef0f6891b2d3a0d81f9c32a59ef053d500882694f87b9f9c464d12df5e6a1df

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8f235b93ae84a8786d24e2d58ca699eebcd0ac5495cd545feb4612d1501193ee
MD5 05f2f9cf2c460d51a8ad96d341079252
BLAKE2b-256 0bd8de07d19be1c337ff795e8049b0e8a24d0677ac6933dcdba962b8dbf92644

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d872f060bbb41c716a63d122ec05c83e60c01d3aa13649257f9ac6107cbd57c4
MD5 502b44f65809cdac6edefbab688348ef
BLAKE2b-256 9c25177a381819c80716ef3e58690e47e75fb779d178b10f97429a3efe43e266

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e33e99f53291f4245476ccd4e5bef5e0ef89cf2a38957d9dd3eb79710eb2ade0
MD5 97eb20aa89756c4f6990e1b90d95f6f5
BLAKE2b-256 b2eefa48b051552f74b1fbd9fa2040bf1693cee035b62da4e26bce44fe8843c9

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 bc47ead114a084e3172cc040caea22a4ad30b8fe9b42fd2a3f0ce69908aaede3
MD5 41016cd19fe21b7c27cc4de89553d34b
BLAKE2b-256 abf534ea15756cdd303b3869e6a721ad3982210983e41abd88f631cb19e41dc6

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.21-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.21-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b57d42a4d77a44eb3f3b153c0b51d619478ea8146cb2fbaddb439acc5a34e0f5
MD5 3943261b06985c012ead9ff65d3d70f2
BLAKE2b-256 60fed832b8e347305d3bd37b846edf4aa192f483fc5b59c243725c54bb8b0dce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page