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
has_git_token Returns whether a usable GITHUB_TOKEN is reachable (used for honest tool listing)
ElementCache Drill-down cache for collapsed elements (code blocks, comments, patches, thread segments) in GitHub discussions
html_to_text Lightweight HTML → plain-text converter (markdown-flavoured)
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
mcp_methods.fastmcp Composable tool registrations for FastMCP servers — see below

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_issue("owner/repo", 123)

# Subsequent calls return cached summary (no network)
summary = cache.fetch_issue("owner/repo", 123)
# → "Cached owner/repo#123 — 5 elements available: cb_1, comment_2, patch_1, patch_2, patch_3"

# Force re-fetch when the issue has changed upstream
text = cache.fetch_issue("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"])

mcp_methods.fastmcp — drop-in tools for FastMCP servers

If you're running your own FastMCP server but want the same tool surface the bundled mcp-server binary ships (source navigation, graph overview, Cypher with CSV export, save_graph), import these helpers and register them on your app:

from mcp.server.fastmcp import FastMCP
from mcp_methods.fastmcp import (
    register_overview,
    register_cypher_query,
    register_source_tools,
    register_save_graph,
    serve_csv_via_http,
)

app = FastMCP("My Server")
register_overview(app, graph, overview_prefix="My custom guidance")
register_cypher_query(app, graph, csv_dir="temp/")
register_source_tools(app, source_roots=["./source"])
register_save_graph(app, graph)
_server, base_url = serve_csv_via_http("temp/")  # optional CORS-enabled HTTP server
app.run(transport="stdio")

Each helper is a thin (~10-line) wrapper over the existing Rust PyO3 surface — there's no logic duplication between the YAML-driven binary and these helpers, so agent behaviour is identical regardless of which path booted the server. graph is any object exposing describe() / cypher() / save(); kglite's KnowledgeGraph satisfies it. A runnable end-to-end stub lives at examples/fastmcp_demo.py.

Deployment — mcp-server binary

This crate also ships an MCP server binary at crates/mcp-server. Build and install it with:

cargo install --path crates/mcp-server
# → ~/.cargo/bin/mcp-server

For deployments that previously pinned a binary path elsewhere (e.g. /opt/miniconda3/envs/embeddings/bin/kglite-mcp-server), drop a symlink:

ln -s ~/.cargo/bin/mcp-server /opt/miniconda3/envs/embeddings/bin/mcp-server

The binary is domain-agnostic — source tools + GitHub access + a manifest-driven tool surface. Downstream binaries (e.g. kglite-mcp-server) re-export mcp_server::McpServer::new(...) to layer graph-specific tools on top while reusing the boot sequence, .env loading, workspace mode, watch mode, and embedder lifecycle.

Operating modes (set via CLI flag or the YAML manifest):

Mode How to set When to use
bare no flag testing the protocol layer in isolation
source-root --source-root DIR or YAML source_root: fixed local directory; no clone
workspace (github) --workspace DIR clone-and-track GitHub repos
workspace (local) YAML workspace: { kind: local, root: ..., watch: ... } fixed local dir + optional file watcher; alternative to the legacy code_review server
watch --watch DIR rebuild downstream artifacts on file changes

YAML manifest declarations win over CLI flags when both are set (same precedence rule as source_root:).

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.23-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.23-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.23-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 24d03b44b9d00a084cb3b73b7381b2a5fccc7b3df1009c60c03d486f9bde23b5
MD5 843988ed92a71bc77de114adc3d5e6e2
BLAKE2b-256 c9f3635268dbf023c7a1254b38084f2b35680ab9bd763ea3a4f09dae99563e86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 1642e86e9b3f7ea11c1b485cca735c218c856b3cff6b8c5dfa30fe5949b9153a
MD5 30a61480f81684ff4c8e26b702104161
BLAKE2b-256 54dcdae706a1e8d52f908e85534c2b63a5163759ceb63a732144ec4eb59b4c29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d82d77d8eed8da49e994340cec88fcab8d1eabe371916c7d3475ac47671dfd91
MD5 084de1da463673e5e45c295385657d5d
BLAKE2b-256 7eea91d3f16b8ef8c216e48945b4742dc79704517d63eb1c5beedb1192fb9504

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6d05ea815d2f86fd49d68521e47acec39862c0c35370d68f52fe3c1c82c2f934
MD5 4abe46a27c9f816424b96b5f475dd784
BLAKE2b-256 2f7cc665a7d6cb2e839d5848660977e289371fc932ef695d82c40acd80491dd3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 5b3d0683aacdd6674daed0784c185c4edf321b1698b3204e04869eeb45358630
MD5 14cb392cd7cf7ebb92b632a1a1877764
BLAKE2b-256 265a1bb75e172206066dbadebb55fe4642f3a87da6ac2ef6f0575d6ae6e97f62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d4670d10614bef3207e83ff677d494adb3394a98ab35a32e8ed58487f1a20712
MD5 7abb38a3927fdd642c020ff11fa7a4c1
BLAKE2b-256 9ae14f219f6f47a6f44e575e38947a9c2b6d8becd3a20679596312d80ecada6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 05ce717fa3f2c67145a03d0c17bd85fce134e366537707993a3e4ac1523add2a
MD5 1beefa656ec21999a05441cde8d6d831
BLAKE2b-256 5f82cc88e2481eefa0757a605231ad5516632a5ee28148c02a616713b535913b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8bf4d2dc7edcc6947b16f1914364b171091ef1050027facf3339fd1c31d3231e
MD5 283d9d876dc4ab185bb5d992323a5fb0
BLAKE2b-256 ce40db491d7de441cfbf1fa7932668d110a6e67b0cb755533f40cd826bfdf668

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6bc22d71df2f62211e429618953369b754d7149f02cfbee12fd18c6310c391b8
MD5 0eb74379fd135e55850a1b484077d6f8
BLAKE2b-256 c6f6ab0710ffbb3a03dba08101ed14d2550cdc9fcd4c74b647df712ab0e4485f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9902b6ff6e0eb673dba03e1a22bf3c8176d1bd66eb02886d37d7e097422edcf6
MD5 34076ffe080df545a893c8a07e8299f9
BLAKE2b-256 f0fa3ca1c72f353aaa6fb3740307104525838c3781b5cca5395a18c7f5b9bd1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 449f20d07a9d6edf0316d90f974d910e3d366327b9b25dad3e70c4c2787a6ce9
MD5 0d5ab79d5ed2dc9e83cd98db5eadb2ce
BLAKE2b-256 d38dcf8133e98ba775a7e8bab97be920abb69b162d402e3d74e82ee39f9a4061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.23-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 61e54347927748d089af07ad90353522b4e4820c3fa9608cd600ccb02250f183
MD5 0701659d56832fc3c6a96381f1e9c362
BLAKE2b-256 6f157e6fbbbc89395cf30c0a86b929790bfd5e1c650bc1ed6a9efc65c2bb3334

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