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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.20-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.20-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.20-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c2e3a314dfdfeed74028c84c6768845540b5a98dcd804818d6468bf79aef9aea
MD5 bdc96626dad144946641a25b6bdcb63d
BLAKE2b-256 ddb443912b3b2bc6ae8df1e8555a2968342c33e6541a72209d32ac3c2932c848

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 fa4faf9810994c2ac736d14893802dc01088ab5bafd4c9440399accc75e31299
MD5 9b480b77ef2a5dd891daa0ebd86c0c98
BLAKE2b-256 cf2dc79d9851fc9f4883c6da9a6469b06f5c6e622f0733c769f7d699a781b29e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 075704b493313dbb5aa94290dbfa461b307a56df50449b4dd4382bb162882150
MD5 d079b87d69f1a5a1cd93242ca12a5798
BLAKE2b-256 c417a2b4ba9908a5cf91e2e99261d2fa1ceef8b69db3780318b8195ae85bc225

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 993f3a2f9d9ed196c5d2db4d12eef6e8b71c14ef63c1944e8989086605d5d7a6
MD5 307ddd453e1fb3545deda9b842e38290
BLAKE2b-256 90c1712890ce37eda025422ca101147924373c8bb3d50f60c824497fead15d34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b11151802c811efcb98e0d0955b8ddc929069b4e6fc2a99a62fc5cc3f2419379
MD5 23d4646fd2c4b29581aba7b76082e305
BLAKE2b-256 cd762d8a8fedf0a936a69b7830035770019f581491d7d16868b3c746e14986ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c5e2fa4adad15baaed52a502ee052e84b79e0339574d1e85ce246842026fe268
MD5 f814bb3fbd9a696d01b65a268914855c
BLAKE2b-256 8de8e2aab91892be279439f097fccdb447b8ce013139de52602bd0e4551a66c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5b656ee3515a07b060d1e4dc4127c5108f3cc726e3fd5f2a402f5688d1a65f98
MD5 7860b2487622f3c39b49b9258cea4a10
BLAKE2b-256 50ed6287ad5c18235cd0424c990588bb38e141e5834099fe0fc8ea0cf39a88f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 9ee0a33b4ab24cc128de0c486f7557794ae80982dbe25d99ca7df13ff793ea02
MD5 c99a9d73b5660fcc11ecc5e53d2feb0b
BLAKE2b-256 377f07229246294128ee6fa92c597a416421d9a5bc7b91a31d48c1c714a2167c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8ae9d40982f22bb81f09ba206d03e0bf6a2f71c2073f1f50ff42f11b26d203f4
MD5 fdb84065f90365084e1ecd796c1d7177
BLAKE2b-256 9524bd36821ff8974079e825fc87655537d0e8dddec8d0504ea343842ceab540

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e10d0b5d7ff5f66949f1ab175dfb0e82542777c50b87089a175fd773ce4ac55f
MD5 96e3c8311186c0f2eb7a82a5a5ac08ec
BLAKE2b-256 ec8d7286848f58c68e29d1ff2f132da0ef749bd7a40d1c4a03cc635a6efc2d66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 5687a4a5a884b8c202a8025ed7e5805692b4151321b4816b56f141ad31b74f59
MD5 3f36289cc81cd801cbd4bf5d154c0c48
BLAKE2b-256 4023b58962f48c362b3fd9d63e2a3d36cc1461c901d71eb9b4eac971941776ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.20-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8aadce80a5e05e4d136b13cfa55127897118611160dfa3843ec51878687e5b2a
MD5 10aaf515485df5aca6e80722976f8d9d
BLAKE2b-256 0a1d39e69298829b182fe1494ef9323635d62fddf52912701855a5dd1494e2c8

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