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) 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, expand=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.

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

Uploaded CPython 3.13Windows x86-64

mcp_methods-0.3.4-cp313-cp313-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

mcp_methods-0.3.4-cp312-cp312-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.12Windows x86-64

mcp_methods-0.3.4-cp312-cp312-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

mcp_methods-0.3.4-cp311-cp311-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.4-cp311-cp311-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

mcp_methods-0.3.4-cp310-cp310-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.4-cp310-cp310-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.4-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.4-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d8ae78b5a07b5b54416844ed0bc494cd835f91aebdd1664e9df5e0b16a303c23
MD5 cc066d25a998f77c56656177ec8bf201
BLAKE2b-256 6d98dc3a8766e5f5c832c07ab865d92edd71a4f19e12b160f24ea47bd43f11de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8568fad053cbf62e0fddadea06cf2f46f50ad8896c449a08f8e4f1abbbe0dd5b
MD5 2ac1902f296110ab4839d702861e8568
BLAKE2b-256 a2c33569ba6e4b9d84f9bd3402219ffa7f2b7d15436c1d48422ca3093d8a61c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 11df93336aeefaa52f9dd00af753c9af139faa0ec7005af55f2b7ea043b937d9
MD5 214539e13cc12b83c16d77206a05d2f4
BLAKE2b-256 58596492a5d0ac1917c0f5f4dcda73736f3c92a11961f93971f4e59066d97ed1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 767dbb0dcb0ccbdaaaa501f091a0d5f2b2fe84e849aa072f990b7178d6b743c7
MD5 02a3f2eef36448abd66f94189539cf80
BLAKE2b-256 074d5d540afa99f223c383e304d7750ce2d09982754a5b698e9daafaa2a8e065

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 2ce105011e6f03d8031e212c8998389332f73369fd2d38cd74255b75b7b42f6d
MD5 83ff82c3011449d0a7fed981d1e948ec
BLAKE2b-256 563b538b6ef6b19284da600497c247d622e2ceebda65f6cf6e43eda9c2cfab5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ab264ed5413204f8bd2613a803f01a286d005a96a100fa0647c3fb49c755b5be
MD5 e2bd16137d5cfc2d169e8670a8966f49
BLAKE2b-256 d5288ba5edba37e0d5decf310b3961db0d3de962ea0e03b3278370d66ae66248

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f3ec5bed1be20c9390140734e5f26730a663ad43d441c1a334e14ab890b9ea11
MD5 32e03a5df652afe5dc8a27c48feab9b3
BLAKE2b-256 e2542ea3ec499ddcbf2620352698d2595fd64e7e3cef0473b93e03c46526c28c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a1b17124567b9d749a5f3fad97ac8c034c3d0b8cfd88f85a78de570013e39704
MD5 1b505084904b9dcd8f157f23e8040da1
BLAKE2b-256 d116ec76c62aa4a2ece75bd9e51ac7ed0855ef3aed22ea5c0d944e9648b8f874

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b71d19fcc5873f1ae7c4b20a3ecdefde20a92678738305b70fc27deffe758ac1
MD5 831a8f3086c85c41299df5af4a40bd90
BLAKE2b-256 920dea321a8402a19c2c837039ddbd37893d9640ab9661ddf5ac11a5ee56e51c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 51ade80836dc4fdfa9040e65d16700b0999764382dc4dd69690316d33ee33730
MD5 20b2c19fbaeac6a0e9b802af50ebbf1d
BLAKE2b-256 641a1c87a312ea5bc2e33bda0632fd0c954df5b4e7c0b0eda1defeca61948469

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a87829cf3696f06078f2af61a1f9ffbab079e321e3e3c0cf0b93c7efd27595f8
MD5 44a20fbce1f92eb7186afd01bf8c4c70
BLAKE2b-256 4c4497286f2ea17f9d896c9f64d489305e2f4a80a341b18e824fc9ac07065fa5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 771fbedc25f8bda5e027636072f02d28b3dc33c6d535ce71dcacf078c861ba56
MD5 c5028b66a3c949400e9a06895958b0b7
BLAKE2b-256 ca6eb3b5c7e4935f96c4c5dd6fe838354755fce4fadfeafeb3239311f16c03a1

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