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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.3-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.3-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.3-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 92baf964645e5f961117fac3fd25c4c6c6aeb39f1829c9eae750ae9d58364fa3
MD5 380edf1fda4d940f54d3841d8ea52386
BLAKE2b-256 79fdba1df9ad402916fcfbfdc87a3c4dccfd2e179759fffe4c5f345c22fb2ba8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 284fc3ebcadc94e521ba983833a5b769b71db2dddabf1a9fde4fded1a6d5b976
MD5 804778817a821e8d5e59734ba29acdea
BLAKE2b-256 752c59da11ce2138efec1f6a971ddbcf1b958cb96c11c9d613fc6d51ec2b021f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 702c85c0f97b0e09c413519f391ff07d9ad68c9afb9ea908db0eff6663d56b1e
MD5 4928180b7edfebfcc0416f342e631614
BLAKE2b-256 c3affd6fe1fdfda04211d767e75a4f728aff1dc66daffa2465822b562ee18000

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ae4360cd3471454c1b063473240b901398fd2912adc2a08d3f16fd9c8b3cc0d0
MD5 e388fccc9e6854fd1c719c9821936b45
BLAKE2b-256 86895fd1562e63396f235bb3e0321ffd95da93705674fb292ff18361636ab8fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 62ebbea1a8b91ec257b20447bb29edf46e585f187675bae5826d6b1eeeb2b724
MD5 764b6eff1b4f4ca36abafd0c4f4cc95a
BLAKE2b-256 faeeb627990c23c2477f93f4faa9633fd4bb0daf1ffe3a184bd38a0b35c9f3c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6ff5cc3fc67fee2558e57d9a85868a04c54219821acb7954aee8f752e57a46bb
MD5 030059d3c2d0d00a29d482dd2586f3b9
BLAKE2b-256 4ff1eb1e0179a128e6f56ab04a49fdfbc9fb474ba8bd70d65b50866ac68fa0e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e18b941db1edae2d9d417a2ea02d8cf9e609700964da92463d47e3560052d109
MD5 fa4ec7a12968cd55f89f76b539a873be
BLAKE2b-256 f703896aa9eade17cf5181aa712bc960c16d3e543260db502610b92fb574b76b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 709eb4a8e9a3796551f59c3699b00249ade2f167a674ad94453c1a8dfbe0133f
MD5 8828417a3db1ede6f3de9789be7a6fe1
BLAKE2b-256 a5e49be6f8541e2f945e5f670367e8d2bf18167c93cb77b0fc3e979ea7409719

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f29d2039451b2dc83baacd3547837f386d1e11ac6c859b50089140a80c0d03ff
MD5 ed5636ebdb588cdc56d7c626319a6d2f
BLAKE2b-256 24bb6dca69960d20ae00c72d05ecc199a17163a9a16c74c22b47f6c2d69b6d73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 87e2e1762e6284b4f4b01548553eaf172537d06f0189a4ae301eb1ff6bf1a675
MD5 5ea9ea1f6f207d1aa9a0e5685629f997
BLAKE2b-256 8d0afa670bab7c1453be7eadb50ff7d83e9a1c19a774b57c3f4ab2d30519f9dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 43da77e668173f82abec34801a93f5b81cb57c360e3e101de80d03911ce5c209
MD5 7c14c5501382adb0894b83d4e90f1099
BLAKE2b-256 a15caaef92c25fcd4ff9271f32dc645866e16dc43eee16a5ef234a58799abb48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ffeab5e8aabb8fbae8abc71a600f9b620f09173d491ce68857d9a6eab105aebc
MD5 d6296411420792413bb1d3905f12715f
BLAKE2b-256 d895758329427f88a3790164e1629ba08043410a365f5ffe01423edb67195180

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