-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_knowledge_server.py
More file actions
215 lines (185 loc) · 6.45 KB
/
mcp_knowledge_server.py
File metadata and controls
215 lines (185 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""MCP Server for local knowledge base search.
Provides search_articles, get_article, and knowledge_stats tools
via JSON-RPC 2.0 over stdio protocol.
"""
import json
import sys
import os
import glob
from pathlib import Path
ARTICLES_DIR = Path(__file__).resolve().parent / "knowledge" / "articles"
def load_articles():
"""Load all article JSON files from the articles directory."""
articles = []
if not ARTICLES_DIR.exists():
return articles
for path in glob.glob(str(ARTICLES_DIR / "*.json")):
try:
with open(path, "r", encoding="utf-8") as f:
articles.append(json.load(f))
except (json.JSONDecodeError, OSError) as e:
sys.stderr.write(f"Failed to load {path}: {e}\n")
return articles
def search_articles(keyword, limit=5):
"""Search articles by keyword (matches title and summary)."""
keyword_lower = keyword.lower()
articles = load_articles()
matched = []
for a in articles:
if keyword_lower in a.get("title", "").lower() or keyword_lower in a.get("summary", "").lower():
matched.append({
"id": a["id"],
"title": a["title"],
"source": a.get("source_platform", "unknown"),
"score": a.get("score", 0),
"tags": a.get("tags", []),
"summary": a.get("summary", ""),
})
return matched[:limit]
def get_article(article_id):
"""Get full article content by ID."""
articles = load_articles()
for a in articles:
if a["id"] == article_id:
return a
return None
def knowledge_stats():
"""Return statistics about the knowledge base."""
articles = load_articles()
total = len(articles)
source_dist = {}
tag_counter = {}
for a in articles:
source = a.get("source_platform", "unknown")
source_dist[source] = source_dist.get(source, 0) + 1
for tag in a.get("tags", []):
tag_counter[tag] = tag_counter.get(tag, 0) + 1
top_tags = sorted(tag_counter.items(), key=lambda x: -x[1])[:10]
return {
"total_articles": total,
"source_distribution": source_dist,
"top_tags": [{"tag": t, "count": c} for t, c in top_tags],
}
def handle_initialize(req):
"""Handle MCP initialize request."""
return {
"jsonrpc": "2.0",
"id": req.get("id"),
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {"tools": {}},
"serverInfo": {
"name": "knowledge-server",
"version": "1.0.0",
},
},
}
def handle_tools_list(req):
"""Handle MCP tools/list request."""
return {
"jsonrpc": "2.0",
"id": req.get("id"),
"result": {
"tools": [
{
"name": "search_articles",
"description": "Search articles by keyword in title and summary",
"inputSchema": {
"type": "object",
"properties": {
"keyword": {
"type": "string",
"description": "Keyword to search for",
},
"limit": {
"type": "integer",
"description": "Maximum number of results (default 5)",
"default": 5,
},
},
"required": ["keyword"],
},
},
{
"name": "get_article",
"description": "Get full article content by ID",
"inputSchema": {
"type": "object",
"properties": {
"article_id": {
"type": "string",
"description": "Article ID (e.g. gt-20260510-001)",
},
},
"required": ["article_id"],
},
},
{
"name": "knowledge_stats",
"description": "Return statistics about the knowledge base",
"inputSchema": {
"type": "object",
"properties": {},
},
},
],
},
}
def handle_tools_call(req):
"""Handle MCP tools/call request."""
params = req.get("params", {})
name = params.get("name", "")
arguments = params.get("arguments", {})
if name == "search_articles":
result = search_articles(
keyword=arguments.get("keyword", ""),
limit=arguments.get("limit", 5),
)
elif name == "get_article":
result = get_article(article_id=arguments.get("article_id", ""))
elif name == "knowledge_stats":
result = knowledge_stats()
else:
return {
"jsonrpc": "2.0",
"id": req.get("id"),
"error": {"code": -32601, "message": f"Unknown tool: {name}"},
}
return {
"jsonrpc": "2.0",
"id": req.get("id"),
"result": {"content": [{"type": "text", "text": json.dumps(result, ensure_ascii=False)}]},
}
METHOD_HANDLERS = {
"initialize": handle_initialize,
"tools/list": handle_tools_list,
"tools/call": handle_tools_call,
}
def main():
"""Main loop: read JSON-RPC requests from stdin and respond on stdout."""
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
req = json.loads(line)
method = req.get("method", "")
handler = METHOD_HANDLERS.get(method)
if handler:
response = handler(req)
else:
response = {
"jsonrpc": "2.0",
"id": req.get("id"),
"error": {"code": -32601, "message": f"Method not found: {method}"},
}
except json.JSONDecodeError as e:
response = {
"jsonrpc": "2.0",
"id": None,
"error": {"code": -32700, "message": f"Parse error: {e}"},
}
sys.stdout.write(json.dumps(response, ensure_ascii=False) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()