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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.9-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.9-cp311-cp311-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.9-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.9-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.9-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bcee831a763447e796067256e755ce4324232ded8efdc77f93aa3d87de045187
MD5 59bb2b0518dda6b56e9c61b770fa2a2f
BLAKE2b-256 d88146aa94c8a6265dca443e2957cc1c244734a559a24e32c2a814b469ba8a37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d1a0a401a2c98590896eb866fbb419a205a31dbbbc6303df1f65446850990aec
MD5 1f3575821ca5a6acd5b2ffedde17b295
BLAKE2b-256 8c81e95a77d78774178576b1ae20e9485a5ee8ed546a12cb54f5311db6b4bf12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 58c61163a19d50973d18a11ab4ea292dcd515f3318c6565b921795f235e26a04
MD5 41de65d709e0ab88fc724984439c2dc6
BLAKE2b-256 a3c1e304889669db5f9aec62de09697ba05d125ee8fffa03893ee74ea11e860a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 304fc9faa2f10426a9a8720f0272fd355121320509b42f96237aa966adb3b5ad
MD5 2f95503172df3bf2d314d41912fce61e
BLAKE2b-256 07bf2ff1f824f402ed4578f5c191fb0edd0d297cdc0a3f6a7a104d3fb552b9b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 0989401424247fafe6289337fd294d70769218420d1c892b0531d79c56e3fe64
MD5 d140b509216befd4ea60671f0db7e7a8
BLAKE2b-256 6ff2b6b3d7cc3d8f3349b41b3811d70e06158123ee6132567c048ad6d8210c55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fee15ee0601160fbd6c9c97d2c489a3d1ec6d146dc2b7ed72fc43c223bf36e6a
MD5 8c46fc6bc7f4bb2bbd73ac1c1fc90d2f
BLAKE2b-256 93e4a5e74a1ab84a9463f43dd166e3958051363c80b66c82babdba303a587271

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6d238a895888c3c843d59c6f114b27cecf8db000ef2cb082c16d34fca81738df
MD5 e87d23be2f8806bdfc310d94c2cb031a
BLAKE2b-256 0950b66b4ceb0be92b823522728ca303cbd09ef111f0dcd6011f6bd280747a45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d87916d1845b7c7218121b6fbba57c4b5afe8f0bff6a2195f7146994693cf8c6
MD5 99349a3ebaa78bba71d917cdc30257da
BLAKE2b-256 13cb4363444e320c0c62a54cad9ed477e582f505d1655f5227aa7663b30206b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 82659d16df6b9f4e9ff0b8baf2ccc557a9e1241a5a30014e1f38f7a9d465b1a2
MD5 2919cf2cb128534b29e9d56869ea8697
BLAKE2b-256 8d81739f12d5f2427a4808ec1d37f457bd3b30defe4354c0dfb30d29a51e61c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8776a4fc23c40ddf421abda6d94e133a03763577912a657fa8dd83edd52da95c
MD5 71b7843d19ee7d7f5fd99f746ab80beb
BLAKE2b-256 16544bd0862387e4967629b9d709851cb04a0dd531dbff6867f93518bb597711

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 25e260045e47cef1a3954d305ea9915d90e48e97b5d4daf48753f0ec705fc622
MD5 addd4c4428ad4c8279a34cc5ea5421cc
BLAKE2b-256 7e9ce5a538b20a29423ffd8c097c5c3b2bde8a6fb1156ea4f0665a7e222538de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.9-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 60507988e18e0789858954b17e09de36a401f744b8df173b7c7c7fd61ce5561c
MD5 78be607120ed3409d6493d7086ffc27e
BLAKE2b-256 ed79c932ee3f39715a0ee418c0f27d1fbf12c2e70d7eebcea333ea289d1160fd

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