Loader

mcp_tools.loader

Load templates from various sources (local files, directories, URLs).

fetch_url(url, timeout=30)

Fetch content from a URL.

Source code in mcp_tools/loader.py
17
18
19
20
21
22
def fetch_url(url: str, timeout: int = 30) -> str:
    """Fetch content from a URL."""
    with httpx.Client(follow_redirects=True, timeout=timeout) as client:
        response = client.get(url)
        response.raise_for_status()
        return response.text

is_url(source)

Check if source is a URL.

Source code in mcp_tools/loader.py
11
12
13
14
def is_url(source: str) -> bool:
    """Check if source is a URL."""
    parsed = urlparse(source)
    return parsed.scheme in ("http", "https")

load(source, pattern='*.md')

Load templates from a source.

Supports: - URL: https://example.com/template.md - Local file: /path/to/template.md - Local directory: /path/to/templates/

Parameters:
  • source (str) –

    Path or URL to load from

  • pattern (str, default: '*.md' ) –

    Glob pattern for directories (default: "*.md")

Yields:
  • Template

    Template objects

Source code in mcp_tools/loader.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def load(source: str, pattern: str = "*.md") -> Iterator[Template]:
    """
    Load templates from a source.

    Supports:
    - URL: https://example.com/template.md
    - Local file: /path/to/template.md
    - Local directory: /path/to/templates/

    Args:
        source: Path or URL to load from
        pattern: Glob pattern for directories (default: "*.md")

    Yields:
        Template objects
    """
    if is_url(source):
        yield load_from_url(source)
        return

    path = Path(source)

    if path.is_file():
        yield load_from_path(path)
    elif path.is_dir():
        yield from load_from_directory(path, pattern)
    else:
        raise ValueError(f"Invalid source: {source}")

load_from_directory(directory, pattern='*.md')

Load all templates from a directory.

Source code in mcp_tools/loader.py
46
47
48
49
50
def load_from_directory(directory: Path, pattern: str = "*.md") -> Iterator[Template]:
    """Load all templates from a directory."""
    for path in directory.glob(pattern):
        if path.is_file():
            yield load_from_path(path)

load_from_path(path)

Load a template from a local file.

Source code in mcp_tools/loader.py
25
26
27
28
29
30
31
def load_from_path(path: Path) -> Template:
    """Load a template from a local file."""
    return Template(
        name=path.stem,
        content=path.read_text(encoding="utf-8"),
        source=str(path),
    )

load_from_url(url)

Load a template from a URL.

Source code in mcp_tools/loader.py
34
35
36
37
38
39
40
41
42
43
def load_from_url(url: str) -> Template:
    """Load a template from a URL."""
    parsed = urlparse(url)
    name = Path(parsed.path).stem or "template"

    return Template(
        name=name,
        content=fetch_url(url),
        source=url,
    )