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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.11-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.11-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.11-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3e2135680730dc27c3d99535f347e854925d5802b202dd70e4acf1013a1de51a
MD5 cb51fc160ebffd57bce592c0f2d6c379
BLAKE2b-256 9bd489c0cecd193be58060bb42b4169846ecf7394fa6586e43bbc2c56aa50183

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d954f84801a2a0d4f792d3e5b6e6cb638bc3c24904ad089c93f48a184533af08
MD5 38249327523bc9c35c9da0b346016bea
BLAKE2b-256 05bcdc67a3e32e9ab43813473ec5a1db3ce969643c1f517c78cb5d38d1acf84b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 69a18517ec67b713a1ab63dc17a075531ff1be8cf8cfb53111465c6dab47d77e
MD5 de94d56d3a5925ebbecc1bc4c1af4a92
BLAKE2b-256 81dfc974e9d665be7cdb019a84554d76e4ff6abc8ef9de00e1b8431b26de40cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e35c5759126dc3408fa8a4e6e9280146597da5d9ce94c326f8b6cdbadd4f2ac5
MD5 19799f7713acdf5a8eee20474a39178d
BLAKE2b-256 12f1e136fc95de61194f6d6634c546214f4afac1734545714018415ba2fbe6a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b0b8a3669bfb91bf2e23dda6e9cfc0d34997fe5bb38e6dc90de055fd2a32e0d1
MD5 009443c7a70b29e77bb61d8c14207e46
BLAKE2b-256 0d41effc49978e9782d0dcc02144789657ef463fd0f04253cce00a7a615b44dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb317f67cef1308c8bbfaaef71cc897e340aa119db0c445ae0a40106a2455e22
MD5 472710d57ad2aeb0f00c88d4896397f7
BLAKE2b-256 f0ec66dc256a603a60909ad5791810e0e8f6491e2b572cbfc302f46045ab3fdd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a7684627f328c8d8ad22dafd653e7636f02ecb7239b2b942533f8d5f82dad638
MD5 9513eaa791b92d7caa4f5b3d18a8f48c
BLAKE2b-256 062b6c60cc4efd16f0e66fd5dfa39dfcecd0b1b6085c7d0440df64b1ad765f16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3cc78925ae798960eed0a7440496d35b884bbf8968510586ff42d8d66f6dcae1
MD5 5a8eb6bc0f4f7e9a904a9829dbc528da
BLAKE2b-256 5802ab1e0c3c010ab63f09d5a61246b91644bfcb27a7b82221730a3a71af52d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8de45ad843f6bbfa3000f17ec150cc13933b630aaad9e65868bad77427482aec
MD5 637a6baa9449673f191abd18645d23ab
BLAKE2b-256 114b0ff76b12b20b92db717665e591ae4ffecd6a7388821a95d86685493aa748

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 136ac4cc228866b3fcb50da6f0f512400d20d75d80da746f1eaa1b45ec974c00
MD5 84c2bcde8a21b535f56439b31dc85fad
BLAKE2b-256 158a8a811d01ef4b83f6f8bc32fca6efc7a8b7acbc9069e96e1f607824353c3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 746cea7f0aae1ef02ce171803164ed5d2f6146fbc19ff632509e0d447aba21b5
MD5 e7e52d6e28142486e7511fb1094a0a93
BLAKE2b-256 e074499ef754e75b1a592d7f5abb69e9857ca7c1ca16f4d29732f1a3ba8cf2c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.11-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e82c02d122cf6d006c395f501f806472e723730ca2f2f5ca2635125b12d8c3b4
MD5 f92fdcb35a467621354596fa52cf70e9
BLAKE2b-256 0d185ee4dfc0397f13828468ce61af01bb596e16c4e58314f9fe8658e92e68f3

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