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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.5-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.5-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.5-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 faf390103247415591c810ac2d3470a3641fca277534fd0ad33debc9b78c2eef
MD5 8a41409be84edbaa3d76754985fcd4a5
BLAKE2b-256 dff74b5f2df1e31fa885cad0e99fd18d8fdeb3efbb5b6faab8d55d72bd213aaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 5e3634b9f9f7f81cb8d6f99c79c73536c27d5ad80f5959a5bae1e10f2abe6e57
MD5 b563b601fe6e3026ae3e70431d8e08f8
BLAKE2b-256 59634ec00338ea3d2aa9cb21fcf211ce15eb7a2f3b1a8e1a6d24cb423dafe24f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9eebbc406aa3557eb9fe40081a7167d74b441d8737fed3b2ed381d01d4a73867
MD5 7d5badd667075b36617c79c57c9cf31d
BLAKE2b-256 f527b10b899c1bfc2a05ac1e075b51fa876556f3146d61191a7bb4163b60ff38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 aa184ed324e827eb0de2f31856669103b75b5fba1f5c00379dc40afc96702bd4
MD5 33b5f4e4c31e0f6661665fe196c32d22
BLAKE2b-256 4c3ec76831d252581cbda8501223bb5d6695bbeff8ab9d297241e0fc4ea06e2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 1844bcebf0631c654c41296469587744226249fabbd6255ec94751941aded730
MD5 4a58ae2dfcfe799eada9bba731ac32cb
BLAKE2b-256 a0a7afdff34fdf2b731a44ddd2d45334afc7faf1a7709bd289d9b1911f9efbd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2299d209d8d0439416ade3b59287c67ec526d3a2065c036c571fdcfe66666323
MD5 d64cdd91231865165f4b48e267139050
BLAKE2b-256 31288122fc2d12b3d3d4b61fd5b81059c26e92dab7acbb329e6f0c5ce57a0e5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0e8f0940198c4e559e1a029248a075f43f5506f4be0d57fa937bbcb074084241
MD5 61bd6802e363f2870050dcd76aab0c7d
BLAKE2b-256 33f3bd061d604ebfa278aa7cd7c0d1405ae51822afe5def763e3a680f06e67fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 43f3822a0f151a603dbb138a16043f4182be866f7985cd6b42fb65286d1d47fd
MD5 098c6a06601c225718a0801d4c779bda
BLAKE2b-256 107067342c6e98aac3e0d3a9eeb4ca80fddfe1e3004081cc88159cf5f6258ead

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 08ccf3ed30aca354aa4666d96a925884224255b2713b9c77804f6adaf35d548b
MD5 00b5eab8e38278aa7ca176c6103e8891
BLAKE2b-256 f98d93399f55a1e2c779fecfb2ffeed53a76bf2ca6047af0d75d17b51839a75b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 12e4a6581b44194fd7943c39b32cc1a8b11055accfdaa1ac365e68352c753abf
MD5 6a07027d40817ca20e6593b3ddf4cc6f
BLAKE2b-256 0a6bcfa8faf38daadbb57abfd390f262a21c54ddb3eca2a6dad6ee26aa244e5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3f16697964cf60d7c17923aea636f41f275c525eb8f2fcdae4051b1a1e1ec202
MD5 efb89f72ed3d9800522158bb84dee324
BLAKE2b-256 f7d98f7678b399141b5fd3a77cebca0e265c476a30e2e3303bcc7f73e82f95a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d628f308392bd47ec7ff3d0ac7c4c065af95ea30cd527dbb892e185cb48b70fc
MD5 06b4e6350da03bad18d3b8d39f8cdccb
BLAKE2b-256 6a8ec18c520879c859665bb6a156ed73ef9d5cdf0ba57b54283f5a192cbfdb88

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