mcp-kb 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mcp_kb/server/app.py DELETED
@@ -1,201 +0,0 @@
1
- """FastMCP application that exposes knowledge base management tools.
2
-
3
- The module builds a :class:`FastMCP` server configured with the knowledge base
4
- operations defined elsewhere in the package. Using FastMCP drastically reduces
5
- protocol boilerplate because the framework introspects type hints and
6
- Docstrings to generate MCP-compatible tool schemas automatically.
7
- """
8
- from __future__ import annotations
9
-
10
- from dataclasses import dataclass
11
- from typing import Iterable, List
12
-
13
- from mcp.server.fastmcp import FastMCP
14
-
15
- from mcp_kb.config import DOC_FILENAME
16
- from mcp_kb.knowledge.events import (
17
- KnowledgeBaseListener,
18
- KnowledgeBaseSearchListener,
19
- )
20
- from mcp_kb.knowledge.search import build_tree_overview, read_documentation, search_text
21
- from mcp_kb.knowledge.store import FileSegment, KnowledgeBase
22
- from mcp_kb.security.path_validation import PathRules, PathValidationError
23
-
24
-
25
- @dataclass
26
- class ReadFileResult:
27
- """Structured output for the ``kb.read_file`` tool."""
28
-
29
- path: str
30
- start_line: int
31
- end_line: int
32
- content: str
33
-
34
-
35
- @dataclass
36
- class RegexReplaceResult:
37
- """Structured output describing the number of replacements performed."""
38
-
39
- replacements: int
40
-
41
-
42
- @dataclass
43
- class SearchMatchResult:
44
- """Structured representation of a search result with contextual lines."""
45
-
46
- path: str
47
- line: int
48
- context: List[str]
49
-
50
-
51
- def create_fastmcp_app(
52
- rules: PathRules,
53
- *,
54
- host: str | None = None,
55
- port: int | None = None,
56
- listeners: Iterable[KnowledgeBaseListener] | None = None,
57
- ) -> FastMCP:
58
- """Build and return a configured :class:`FastMCP` server instance.
59
-
60
- Parameters
61
- ----------
62
- rules:
63
- Sanitised filesystem rules that restrict all knowledge base operations to
64
- a designated root.
65
- host:
66
- Optional host interface for HTTP/SSE transports. ``None`` uses FastMCP's
67
- defaults.
68
- port:
69
- Optional TCP port for HTTP/SSE transports. ``None`` uses FastMCP's defaults.
70
- listeners:
71
- Optional iterable of :class:`KnowledgeBaseListener` implementations that
72
- should receive change notifications. The iterable is passed directly to
73
- :class:`~mcp_kb.knowledge.store.KnowledgeBase` so that integrations such
74
- as Chroma ingestion can react to file lifecycle events.
75
- """
76
-
77
- kb = KnowledgeBase(rules, listeners=listeners)
78
- search_providers: List[KnowledgeBaseSearchListener] = []
79
- if listeners is not None:
80
- for listener in listeners:
81
- if isinstance(listener, KnowledgeBaseSearchListener):
82
- search_providers.append(listener)
83
- fastmcp_kwargs: dict[str, object] = {}
84
- if host is not None:
85
- fastmcp_kwargs["host"] = host
86
- if port is not None:
87
- fastmcp_kwargs["port"] = port
88
-
89
- mcp = FastMCP(
90
- "mcp-knowledge-base",
91
- instructions=(
92
- "You are connected to a local text-based knowledge base. Use the provided "
93
- "tools to create, inspect, and organize content while respecting the "
94
- "soft deletion semantics and the protected documentation folder."
95
- ),
96
- **fastmcp_kwargs,
97
- )
98
-
99
- @mcp.tool(name="create_file", title="Create File")
100
- def create_file(path: str, content: str) -> str:
101
- """Create or overwrite a text file at ``path`` with ``content``."""
102
-
103
- try:
104
- created = kb.create_file(path, content)
105
- except PathValidationError as exc:
106
- raise ValueError(str(exc)) from exc
107
- return f"Created {created}"
108
-
109
- @mcp.tool(name="read_file", title="Read File", structured_output=True)
110
- def read_file(path: str, start_line: int | None = None, end_line: int | None = None) -> ReadFileResult:
111
- """Read a text file returning metadata about the extracted segment."""
112
-
113
- try:
114
- segment: FileSegment = kb.read_file(path, start_line=start_line, end_line=end_line)
115
- except PathValidationError as exc:
116
- raise ValueError(str(exc)) from exc
117
- except FileNotFoundError as exc:
118
- raise ValueError(str(exc)) from exc
119
- return ReadFileResult(
120
- path=str(segment.path),
121
- start_line=segment.start_line,
122
- end_line=segment.end_line,
123
- content=segment.content,
124
- )
125
-
126
- @mcp.tool(name="append_file", title="Append File")
127
- def append_file(path: str, content: str) -> str:
128
- """Append ``content`` to the file specified by ``path``."""
129
-
130
- try:
131
- target = kb.append_file(path, content)
132
- except PathValidationError as exc:
133
- raise ValueError(str(exc)) from exc
134
- return f"Appended to {target}"
135
-
136
- @mcp.tool(name="regex_replace", title="Regex Replace", structured_output=True)
137
- def regex_replace(path: str, pattern: str, replacement: str) -> RegexReplaceResult:
138
- """Perform a regex-based replacement across the full file."""
139
-
140
- try:
141
- replacements = kb.regex_replace(path, pattern, replacement)
142
- except PathValidationError as exc:
143
- raise ValueError(str(exc)) from exc
144
- return RegexReplaceResult(replacements=replacements)
145
-
146
- @mcp.tool(name="delete", title="Soft Delete")
147
- def delete(path: str) -> str:
148
- """Soft delete the file at ``path`` by appending the configured sentinel."""
149
-
150
- try:
151
- deleted = kb.soft_delete(path)
152
- except PathValidationError as exc:
153
- raise ValueError(str(exc)) from exc
154
- except FileNotFoundError as exc:
155
- raise ValueError(str(exc)) from exc
156
- return f"Marked {deleted.name} as deleted"
157
-
158
- @mcp.tool(name="search", title="Search", structured_output=True)
159
- def search(query: str, limit: int = 5) -> List[SearchMatchResult]:
160
- """Search for ``query`` across the knowledge base with semantic ranking.
161
-
162
- Registered listeners that implement the optional search interface are
163
- queried first (e.g., the Chroma ingestor). When no listener returns a
164
- result the tool falls back to streaming the markdown files directly so
165
- callers always receive deterministic text snippets.
166
- """
167
-
168
- if limit <= 0:
169
- raise ValueError("limit must be greater than zero")
170
-
171
- matches = search_text(
172
- kb,
173
- query,
174
- providers=search_providers,
175
- n_results=limit,
176
- )
177
- return [
178
- SearchMatchResult(
179
- path=str(match.path),
180
- line=match.line_number,
181
- context=match.context,
182
- )
183
- for match in matches
184
- ]
185
-
186
- @mcp.tool(name="overview", title="Overview")
187
- def overview() -> str:
188
- """Return a textual tree describing the knowledge base structure."""
189
-
190
- return build_tree_overview(kb)
191
-
192
- @mcp.tool(name="documentation", title="Documentation")
193
- def documentation() -> str:
194
- """Read the knowledge base documentation if ``%s`` exists.""" % DOC_FILENAME
195
-
196
- text = read_documentation(kb)
197
- if not text:
198
- return "Documentation is not available."
199
- return text
200
-
201
- return mcp
mcp_kb/utils/__init__.py DELETED
@@ -1 +0,0 @@
1
- """Utility helpers shared across the knowledge base server modules."""
@@ -1,127 +0,0 @@
1
- """Filesystem helpers wrapping Python's standard library primitives.
2
-
3
- The knowledge base server performs numerous file operations. Consolidating the
4
- logic in this module keeps the rest of the code focused on business semantics
5
- such as validating incoming requests and shaping responses. Each helper function
6
- is intentionally small so that callers can compose them for different workflows
7
- without duplicating the low-level boilerplate.
8
- """
9
- from __future__ import annotations
10
-
11
- from contextlib import contextmanager
12
- from pathlib import Path
13
- from threading import Lock
14
- from typing import Dict, Iterator
15
-
16
-
17
- class FileLockRegistry:
18
- """In-memory lock registry to serialize write operations per file.
19
-
20
- Using per-path locks prevents concurrent writes from interleaving content
21
- and potentially corrupting files. The registry lazily creates locks when a
22
- path is first encountered. We reuse locks for subsequent operations to avoid
23
- unbounded memory usage.
24
- """
25
-
26
- def __init__(self) -> None:
27
- """Initialize the registry with an empty dictionary."""
28
-
29
- self._locks: Dict[Path, Lock] = {}
30
- self._global_lock = Lock()
31
-
32
- @contextmanager
33
- def acquire(self, path: Path) -> Iterator[None]:
34
- """Context manager that acquires a lock for the supplied path.
35
-
36
- The helper nests two locks: a global mutex to retrieve or create the
37
- per-path lock, and the per-path lock itself for the duration of the
38
- caller's critical section.
39
-
40
- Parameters
41
- ----------
42
- path:
43
- Absolute path indicating which file should be protected.
44
- """
45
-
46
- with self._global_lock:
47
- lock = self._locks.setdefault(path, Lock())
48
- lock.acquire()
49
- try:
50
- yield
51
- finally:
52
- lock.release()
53
-
54
-
55
- def write_text(path: Path, content: str) -> None:
56
- """Write text content to ``path`` using UTF-8 encoding."""
57
-
58
- path.write_text(content, encoding="utf-8")
59
-
60
-
61
- def append_text(path: Path, content: str) -> None:
62
- """Append text content to ``path`` using UTF-8 encoding."""
63
-
64
- with path.open("a", encoding="utf-8") as handle:
65
- handle.write(content)
66
-
67
-
68
- def read_text(path: Path) -> str:
69
- """Read UTF-8 text content from ``path`` and return it."""
70
-
71
- return path.read_text(encoding="utf-8")
72
-
73
-
74
- def ensure_parent_directory(path: Path) -> None:
75
- """Ensure the parent directory of ``path`` exists by creating it."""
76
-
77
- path.parent.mkdir(parents=True, exist_ok=True)
78
-
79
-
80
- def rename(path: Path, target: Path) -> None:
81
- """Rename ``path`` to ``target`` using ``Path.rename`` semantics."""
82
-
83
- path.rename(target)
84
-
85
-
86
- def is_text_file(path: Path, max_bytes: int = 2048) -> bool:
87
- """Heuristically determine whether ``path`` contains UTF-8 text.
88
-
89
- The check is designed to be fast and conservative for use when iterating
90
- a directory tree. It reads at most ``max_bytes`` from the file in binary
91
- mode and applies two filters:
92
-
93
- - Reject files that contain NUL bytes, which are extremely uncommon in
94
- textual formats and a strong indicator of binary content.
95
- - Attempt to decode the sampled bytes as UTF-8. If decoding fails, the
96
- file is treated as binary.
97
-
98
- Parameters
99
- ----------
100
- path:
101
- Absolute path to the file on disk.
102
- max_bytes:
103
- Upper bound on the number of bytes to sample from the head of the
104
- file. A small sample keeps directory scans fast while remaining
105
- accurate for typical text formats such as ``.md``, ``.txt``, ``.xml``,
106
- and source files.
107
-
108
- Returns
109
- -------
110
- bool
111
- ``True`` if the file appears to be UTF-8 text; ``False`` otherwise.
112
- """
113
-
114
- try:
115
- with path.open("rb") as handle:
116
- sample = handle.read(max_bytes)
117
- except (FileNotFoundError, PermissionError): # pragma: no cover - defensive
118
- return False
119
-
120
- if b"\x00" in sample:
121
- return False
122
-
123
- try:
124
- sample.decode("utf-8")
125
- return True
126
- except UnicodeDecodeError:
127
- return False
@@ -1,26 +0,0 @@
1
- mcp_kb/__init__.py,sha256=Ry7qODhfFQF6u6p2m3bwGWhB0-BdWTQcHDJB7NBYAio,74
2
- mcp_kb/config.py,sha256=VallCc3_Bjcm2FPElthupvXdbMuXvDkiTkQKj0f4dkQ,2506
3
- mcp_kb/cli/__init__.py,sha256=dEIRWFycAfPkha1S1Bj_Y6zkvEZv4eF0qtbF9t74r60,67
4
- mcp_kb/cli/args.py,sha256=0yU5lwjjUkgk91ksocqOdpqO_u5JU6xuCaayiOJ-5pQ,5371
5
- mcp_kb/cli/main.py,sha256=FMsnWcXmEsXXfETyvPMP2il9jREGFWmR8t23-6QfhMo,3864
6
- mcp_kb/cli/reindex.py,sha256=UBBN7_u9rcgGXel5FKnnA3yd7a-AQMwHMwQjt7ZFrSs,3033
7
- mcp_kb/data/KNOWLEDBASE_DOC.md,sha256=bkSpdK1W3F0KR6d3q4V_23fnY8Kw2IBjXPvTTRv06AI,1663
8
- mcp_kb/data/__init__.py,sha256=UYYuO_n2ikjpwkPSykgleiifYvC0V8_O-atUaRBQUm4,70
9
- mcp_kb/ingest/__init__.py,sha256=8obrvfa8nLNLYPbi1MHlFUqfoFHgK9YfdryPzAXQ6kU,77
10
- mcp_kb/ingest/chroma.py,sha256=3Kt7or1Z9ng-wBeXeuOPhrVIfjokwkF25jKHdAeYSH8,22170
11
- mcp_kb/knowledge/__init__.py,sha256=W_dtRbtnQlrDJ_425vWR8BcoZGJ8gC5-wg1De1E654s,76
12
- mcp_kb/knowledge/bootstrap.py,sha256=WlbJUXhxglyWjlvwhUdT20oijLNLaZOePQ6nYwfBCxk,1202
13
- mcp_kb/knowledge/events.py,sha256=A7CfD7U5bxo6mCCIic83yE7VPixAN05ssw_HKRF2zxw,3549
14
- mcp_kb/knowledge/search.py,sha256=AKsyNipsA8bfRxIJb49tdsU4ICzbHeFrA1Ikvlk1u7w,5901
15
- mcp_kb/knowledge/store.py,sha256=urZaLrSRjgqxb_hLC6RQjDeNw8Id14ripQH_6HdWb3o,10002
16
- mcp_kb/security/__init__.py,sha256=lF8_XAjzpwhAFresuskXMo0u9v7KFiTJId88wqOAM4Y,62
17
- mcp_kb/security/path_validation.py,sha256=21bfKdxjHY-ywDYw0DcGCeXDnvdXDILWVuueUsuuZUM,3617
18
- mcp_kb/server/__init__.py,sha256=j9TmxW_WLCoibyQvCsDT1MIuUqSL8sRh2h4u0M4eU0c,74
19
- mcp_kb/server/app.py,sha256=7s10UJWFopJ4CZPqZ4briTKsfjDiRo44ZbLAeWb6Lj8,7064
20
- mcp_kb/utils/__init__.py,sha256=lKhRsjgnbhye1sSlch1_wsAI3eWKE1M6RVIiNlnsvLI,71
21
- mcp_kb/utils/filesystem.py,sha256=0M-Waf2vfqhp8UL__2Emfpwpoqxshti3M7XLjXnpjJw,4026
22
- mcp_kb-0.2.0.dist-info/METADATA,sha256=AjAH4xcztes0PzJkjW3J9qyes0VDXS7jfsJFQMq2s1g,5122
23
- mcp_kb-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
24
- mcp_kb-0.2.0.dist-info/entry_points.txt,sha256=qwJkR3vV7ZeydfS_IYMiDwLv4BdTkrOf4-5neWj25g0,96
25
- mcp_kb-0.2.0.dist-info/top_level.txt,sha256=IBiz3TNE3FF3TwkbCZpC1kkk6ohTwtBQNSPJNV3-qGA,7
26
- mcp_kb-0.2.0.dist-info/RECORD,,
File without changes