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.19-cp313-cp313-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.13Windows x86-64

mcp_methods-0.3.19-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.19-cp313-cp313-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

mcp_methods-0.3.19-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.19-cp312-cp312-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.19-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.19-cp311-cp311-macosx_11_0_arm64.whl (2.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.19-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.19-cp310-cp310-macosx_11_0_arm64.whl (2.9 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0b3f8f0ae175aca8e2e5c5bb468d948d501588f72713d839b9451521491c8af4
MD5 cc9fb05c0a8072822f6cf7996d4acba6
BLAKE2b-256 a2b6da59069d4a1a34c3096356d355bb19d320d188562c351d557839bec5bc8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 4854a3cb1d576f07b3575ab8cc7550e8855671fa10a45b5a7d616b3d938542a0
MD5 f36baad46c44040234480b6d55a7ac06
BLAKE2b-256 d66fd8051b058392b235231ba35af12a0040bd0adb941be15ce551bf614378fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bd202744c5fe3763f4de4f566a481eeb9f534f3df786965fdb8e1858851b433f
MD5 6f5d6b441464d3e99e757be9a081d67f
BLAKE2b-256 0470eda4b0861b3080be78c5ec8915406917d92013cf32a9c05490673449eb59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d0e60626f631c5e2a293b0d7ebd2749ffe87b4c08f521b22ecd6fb2f0218569b
MD5 203e525dab60157cb8d36be353ab92a7
BLAKE2b-256 0dc6cd2f6e82c8c6f60074945cefc47d2d364584c87edc23aa35c5643e562a1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a93cddfda15cb3e05fa38137cad5588deaf808b78e034a5b48995213360731ae
MD5 7b3491b6a646b4d8a2c71a433d93d013
BLAKE2b-256 71ad07451a6780a3a18dbca73abc02725d7ae7801e7ae4eb18f1df2725d9449f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5df4e92998dfe509815465375655c30db3a568524544cd6289c09d591d625a5e
MD5 5dbd1f61870d1404ad18c86ee6f825dd
BLAKE2b-256 03690285b049c614b9fcee9c787a2450ed7a83bce26b9aa0094892934a2203d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 88add08f2bda824f046914b2c3c8eee40a9fbdedb56117fcc9a73ba419bae7cb
MD5 1035dced9551b7618fedef9ac108861b
BLAKE2b-256 d6cf2bf88712dfcf0eba528f37303793f851f73e0a3cb50c51d39758eff18ccb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b1b57a287ec32ef15908c67939b74f58b12cb312b23e55dc59504ccad55bea9c
MD5 5d07aed78689d3d3f233f1b0271028af
BLAKE2b-256 7ec8a243c69a4bf11c3b8903d1c57904757403b81e1b061cbc04fb1d0888b61e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 46dfe88cb909f1043c990744779e66098fb7ce786a5847178bb66dded71cf87d
MD5 fbab9ccd774a53739b45d54d90acc532
BLAKE2b-256 0b4065cc96b6b01264d3b7543f172c3322d4b787367450c9636c0c04f58a0201

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ecc64cf9c5aa22eb6ed3b2a9c27615afb266eaf9f47cc339e232665929eb21ec
MD5 0c2aa3f7f89d9c1191fb429037a5a895
BLAKE2b-256 c5b8f73e93ac9e3feee49be58c7509588168088064bf97bb92a2f81bcf7dd6d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a15e2d9165fdb0b3b0aa0e8a29f43e00370af4bbd9b29f2e027c7f521f441d36
MD5 e24d06d897b2f3f7c8ade3cd0c898e9f
BLAKE2b-256 bce8b8407b528e94093bbe221fb4d7d03608f1288e71f9945e7f4400eee2409a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.19-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3f3759fa742bd85a50ebbc3af20f9c6371df279bd4228d87c889abc0d647341
MD5 eae30d934eceaaff8f078c47e0489617
BLAKE2b-256 4a09dc306b4cda8f929127b6fedcbdf38fcdd40b2fef67fbc2b89968d38c6090

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