git-contribution-analyzer 0.8.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.
- git_contribution_analyzer/__init__.py +3 -0
- git_contribution_analyzer/adapters/__init__.py +1 -0
- git_contribution_analyzer/adapters/git/__init__.py +1 -0
- git_contribution_analyzer/adapters/git/local_identity.py +51 -0
- git_contribution_analyzer/adapters/git/native_git.py +353 -0
- git_contribution_analyzer/adapters/git/pydriller_history.py +57 -0
- git_contribution_analyzer/adapters/git/repository_discovery.py +75 -0
- git_contribution_analyzer/adapters/gui/__init__.py +1 -0
- git_contribution_analyzer/adapters/gui/app.py +63 -0
- git_contribution_analyzer/adapters/gui/controller.py +1084 -0
- git_contribution_analyzer/adapters/gui/models/__init__.py +3 -0
- git_contribution_analyzer/adapters/gui/models/base_table.py +108 -0
- git_contribution_analyzer/adapters/gui/settings.py +53 -0
- git_contribution_analyzer/adapters/gui/task_runner.py +196 -0
- git_contribution_analyzer/adapters/llm/__init__.py +1 -0
- git_contribution_analyzer/adapters/llm/anthropic.py +52 -0
- git_contribution_analyzer/adapters/llm/base.py +142 -0
- git_contribution_analyzer/adapters/llm/cache.py +148 -0
- git_contribution_analyzer/adapters/llm/claude_cli.py +52 -0
- git_contribution_analyzer/adapters/llm/codex_cli.py +52 -0
- git_contribution_analyzer/adapters/llm/command_base.py +153 -0
- git_contribution_analyzer/adapters/llm/mock.py +82 -0
- git_contribution_analyzer/adapters/llm/ollama.py +53 -0
- git_contribution_analyzer/adapters/llm/openai_compatible.py +61 -0
- git_contribution_analyzer/adapters/llm/registry.py +117 -0
- git_contribution_analyzer/adapters/mcp/__init__.py +13 -0
- git_contribution_analyzer/adapters/mcp/installer.py +175 -0
- git_contribution_analyzer/adapters/mcp/prompts_resources.py +218 -0
- git_contribution_analyzer/adapters/mcp/protocol.py +361 -0
- git_contribution_analyzer/adapters/mcp/server.py +52 -0
- git_contribution_analyzer/adapters/mcp/tools.py +621 -0
- git_contribution_analyzer/adapters/outcomes/__init__.py +1 -0
- git_contribution_analyzer/adapters/outcomes/yaml_loader.py +55 -0
- git_contribution_analyzer/adapters/outcomes/yaml_store.py +104 -0
- git_contribution_analyzer/adapters/reporting/__init__.py +1 -0
- git_contribution_analyzer/adapters/reporting/brag_markdown.py +115 -0
- git_contribution_analyzer/adapters/reporting/html_report.py +1413 -0
- git_contribution_analyzer/adapters/reporting/markdown.py +217 -0
- git_contribution_analyzer/adapters/reporting/portfolio_markdown.py +167 -0
- git_contribution_analyzer/adapters/reporting/recap_markdown.py +212 -0
- git_contribution_analyzer/adapters/reporting/resume_markdown.py +37 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_csv.py +117 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_markdown.py +90 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_series_markdown.py +31 -0
- git_contribution_analyzer/adapters/storage/__init__.py +1 -0
- git_contribution_analyzer/adapters/storage/sqlite/__init__.py +1 -0
- git_contribution_analyzer/adapters/storage/sqlite/analysis.py +708 -0
- git_contribution_analyzer/adapters/storage/sqlite/database.py +98 -0
- git_contribution_analyzer/adapters/storage/sqlite/git_index.py +482 -0
- git_contribution_analyzer/adapters/storage/sqlite/identity_merge.py +373 -0
- git_contribution_analyzer/adapters/storage/sqlite/llm_audit.py +109 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/env.py +34 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/script.py.mako +22 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0001_initial.py +51 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0002_git_index.py +146 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0003_analysis.py +79 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0004_llm_audit.py +69 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0005_run_types.py +42 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0006_identity_merges.py +83 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0007_structural_baselines.py +173 -0
- git_contribution_analyzer/adapters/storage/sqlite/models.py +354 -0
- git_contribution_analyzer/adapters/storage/sqlite/structural_baseline.py +841 -0
- git_contribution_analyzer/adapters/workspace/__init__.py +1 -0
- git_contribution_analyzer/adapters/workspace/config.py +135 -0
- git_contribution_analyzer/adapters/workspace/git_exclude.py +32 -0
- git_contribution_analyzer/adapters/workspace/layout.py +40 -0
- git_contribution_analyzer/adapters/workspace/locks.py +27 -0
- git_contribution_analyzer/adapters/workspace/portfolio_workspace.py +380 -0
- git_contribution_analyzer/adapters/workspace/ranking_config.py +17 -0
- git_contribution_analyzer/application/__init__.py +1 -0
- git_contribution_analyzer/application/dto/__init__.py +19 -0
- git_contribution_analyzer/application/dto/gui.py +86 -0
- git_contribution_analyzer/application/dto/results.py +21 -0
- git_contribution_analyzer/application/facades/__init__.py +3 -0
- git_contribution_analyzer/application/facades/gui.py +568 -0
- git_contribution_analyzer/application/ports/__init__.py +22 -0
- git_contribution_analyzer/application/ports/cancellation.py +50 -0
- git_contribution_analyzer/application/ports/git_history.py +22 -0
- git_contribution_analyzer/application/ports/llm.py +44 -0
- git_contribution_analyzer/application/ports/portfolio_store.py +34 -0
- git_contribution_analyzer/application/ports/progress.py +43 -0
- git_contribution_analyzer/application/ports/structural_baseline.py +93 -0
- git_contribution_analyzer/application/services/__init__.py +1 -0
- git_contribution_analyzer/application/services/assessment_explanation.py +127 -0
- git_contribution_analyzer/application/services/default_person.py +61 -0
- git_contribution_analyzer/application/services/evidence_snapshot.py +64 -0
- git_contribution_analyzer/application/services/person_selection.py +88 -0
- git_contribution_analyzer/application/services/resume_generation.py +318 -0
- git_contribution_analyzer/application/services/semantic_enhancement.py +157 -0
- git_contribution_analyzer/application/services/time_boundaries.py +49 -0
- git_contribution_analyzer/application/use_cases/__init__.py +1 -0
- git_contribution_analyzer/application/use_cases/analyze_contributions.py +276 -0
- git_contribution_analyzer/application/use_cases/analyze_project.py +295 -0
- git_contribution_analyzer/application/use_cases/assess_portfolio.py +327 -0
- git_contribution_analyzer/application/use_cases/assess_work.py +602 -0
- git_contribution_analyzer/application/use_cases/assess_work_series.py +212 -0
- git_contribution_analyzer/application/use_cases/compare_assessment_runs.py +121 -0
- git_contribution_analyzer/application/use_cases/export_report.py +49 -0
- git_contribution_analyzer/application/use_cases/generate_brag.py +308 -0
- git_contribution_analyzer/application/use_cases/generate_portfolio_recap.py +244 -0
- git_contribution_analyzer/application/use_cases/generate_portfolio_resume.py +152 -0
- git_contribution_analyzer/application/use_cases/generate_recap.py +316 -0
- git_contribution_analyzer/application/use_cases/generate_report.py +32 -0
- git_contribution_analyzer/application/use_cases/generate_resume.py +209 -0
- git_contribution_analyzer/application/use_cases/get_portfolio_status.py +83 -0
- git_contribution_analyzer/application/use_cases/get_run.py +16 -0
- git_contribution_analyzer/application/use_cases/get_status.py +69 -0
- git_contribution_analyzer/application/use_cases/index_repository.py +267 -0
- git_contribution_analyzer/application/use_cases/init_portfolio.py +89 -0
- git_contribution_analyzer/application/use_cases/init_project.py +71 -0
- git_contribution_analyzer/application/use_cases/list_identities.py +16 -0
- git_contribution_analyzer/application/use_cases/list_providers.py +29 -0
- git_contribution_analyzer/application/use_cases/list_runs.py +17 -0
- git_contribution_analyzer/application/use_cases/manage_identity_merges.py +48 -0
- git_contribution_analyzer/application/use_cases/manage_outcomes.py +189 -0
- git_contribution_analyzer/application/use_cases/manage_structural_baselines.py +262 -0
- git_contribution_analyzer/application/use_cases/map_identity.py +28 -0
- git_contribution_analyzer/application/use_cases/recover_runs.py +22 -0
- git_contribution_analyzer/application/use_cases/run_doctor.py +81 -0
- git_contribution_analyzer/application/use_cases/sync_portfolio.py +232 -0
- git_contribution_analyzer/application/use_cases/test_provider.py +22 -0
- git_contribution_analyzer/application/use_cases/uninit_project.py +23 -0
- git_contribution_analyzer/application/use_cases/verify_portfolio.py +165 -0
- git_contribution_analyzer/cli/__init__.py +1 -0
- git_contribution_analyzer/cli/app.py +1430 -0
- git_contribution_analyzer/cli/exit_codes.py +13 -0
- git_contribution_analyzer/cli/output.py +26 -0
- git_contribution_analyzer/cli/portfolio_commands.py +629 -0
- git_contribution_analyzer/domain/__init__.py +1 -0
- git_contribution_analyzer/domain/errors.py +55 -0
- git_contribution_analyzer/domain/models/__init__.py +3 -0
- git_contribution_analyzer/domain/models/analysis.py +29 -0
- git_contribution_analyzer/domain/models/assessment.py +12 -0
- git_contribution_analyzer/domain/models/brag.py +148 -0
- git_contribution_analyzer/domain/models/contribution.py +55 -0
- git_contribution_analyzer/domain/models/git_history.py +69 -0
- git_contribution_analyzer/domain/models/person_selection.py +46 -0
- git_contribution_analyzer/domain/models/portfolio.py +498 -0
- git_contribution_analyzer/domain/models/ranking.py +48 -0
- git_contribution_analyzer/domain/models/recap.py +235 -0
- git_contribution_analyzer/domain/models/repository.py +10 -0
- git_contribution_analyzer/domain/models/resume.py +59 -0
- git_contribution_analyzer/domain/models/run.py +10 -0
- git_contribution_analyzer/domain/models/semantic.py +34 -0
- git_contribution_analyzer/domain/models/snapshot.py +208 -0
- git_contribution_analyzer/domain/models/structural_baseline.py +120 -0
- git_contribution_analyzer/domain/models/work_assessment.py +81 -0
- git_contribution_analyzer/domain/services/assessment_periods.py +182 -0
- git_contribution_analyzer/domain/services/brag_rules.py +250 -0
- git_contribution_analyzer/domain/services/capability_rules.py +91 -0
- git_contribution_analyzer/domain/services/contribution_clustering.py +98 -0
- git_contribution_analyzer/domain/services/contribution_rules.py +79 -0
- git_contribution_analyzer/domain/services/difficulty_rules.py +191 -0
- git_contribution_analyzer/domain/services/dimensional_summary.py +117 -0
- git_contribution_analyzer/domain/services/portfolio_aggregation.py +250 -0
- git_contribution_analyzer/domain/services/portfolio_resume_rules.py +265 -0
- git_contribution_analyzer/domain/services/portfolio_verifier.py +238 -0
- git_contribution_analyzer/domain/services/ranking_rules.py +226 -0
- git_contribution_analyzer/domain/services/recap_rules.py +268 -0
- git_contribution_analyzer/domain/services/resume_claim_rules.py +70 -0
- git_contribution_analyzer/domain/services/structural_baseline.py +317 -0
- git_contribution_analyzer/domain/services/structural_signals.py +151 -0
- git_contribution_analyzer/domain/services/workload_rules.py +185 -0
- git_contribution_analyzer/gui/__init__.py +1 -0
- git_contribution_analyzer/gui/qml/Main.qml +1141 -0
- git_contribution_analyzer/gui/qml/__init__.py +1 -0
- git_contribution_analyzer/prompts/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/v1/explanation.txt +4 -0
- git_contribution_analyzer/prompts/resume/__init__.py +1 -0
- git_contribution_analyzer/prompts/resume/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-concise.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-star.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-xyz.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/system.txt +3 -0
- git_contribution_analyzer/prompts/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/v1/capability_explanation.txt +2 -0
- git_contribution_analyzer/prompts/v1/contribution_summary.txt +2 -0
- git_contribution_analyzer/prompts/v1/overall_summary.txt +2 -0
- git_contribution_analyzer/prompts/v1/resume_bullet.txt +2 -0
- git_contribution_analyzer-0.8.0.dist-info/METADATA +667 -0
- git_contribution_analyzer-0.8.0.dist-info/RECORD +187 -0
- git_contribution_analyzer-0.8.0.dist-info/WHEEL +4 -0
- git_contribution_analyzer-0.8.0.dist-info/entry_points.txt +3 -0
- git_contribution_analyzer-0.8.0.dist-info/licenses/LICENSE +201 -0
- git_contribution_analyzer-0.8.0.dist-info/licenses/NOTICE +6 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from git_contribution_analyzer import __version__
|
|
9
|
+
|
|
10
|
+
SUPPORTED_PROTOCOL_VERSIONS = ("2025-03-26", "2024-11-05")
|
|
11
|
+
|
|
12
|
+
JSONRPC_PARSE_ERROR = -32700
|
|
13
|
+
JSONRPC_INVALID_REQUEST = -32600
|
|
14
|
+
JSONRPC_METHOD_NOT_FOUND = -32601
|
|
15
|
+
JSONRPC_INVALID_PARAMS = -32602
|
|
16
|
+
JSONRPC_INTERNAL_ERROR = -32603
|
|
17
|
+
|
|
18
|
+
ToolHandler = Callable[[dict[str, Any]], dict[str, Any]]
|
|
19
|
+
PromptHandler = Callable[[dict[str, str]], list[dict[str, Any]]]
|
|
20
|
+
ResourceHandler = Callable[[], str]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class McpToolDefinition:
|
|
25
|
+
name: str
|
|
26
|
+
description: str
|
|
27
|
+
input_schema: dict[str, Any]
|
|
28
|
+
handler: ToolHandler
|
|
29
|
+
|
|
30
|
+
def to_mcp_dict(self) -> dict[str, Any]:
|
|
31
|
+
return {
|
|
32
|
+
"name": self.name,
|
|
33
|
+
"description": self.description,
|
|
34
|
+
"inputSchema": self.input_schema,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True, slots=True)
|
|
39
|
+
class McpPromptArgument:
|
|
40
|
+
name: str
|
|
41
|
+
description: str
|
|
42
|
+
required: bool = False
|
|
43
|
+
|
|
44
|
+
def to_mcp_dict(self) -> dict[str, Any]:
|
|
45
|
+
return {
|
|
46
|
+
"name": self.name,
|
|
47
|
+
"description": self.description,
|
|
48
|
+
"required": self.required,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True, slots=True)
|
|
53
|
+
class McpPromptDefinition:
|
|
54
|
+
name: str
|
|
55
|
+
description: str
|
|
56
|
+
arguments: tuple[McpPromptArgument, ...]
|
|
57
|
+
handler: PromptHandler
|
|
58
|
+
|
|
59
|
+
def to_mcp_dict(self) -> dict[str, Any]:
|
|
60
|
+
return {
|
|
61
|
+
"name": self.name,
|
|
62
|
+
"description": self.description,
|
|
63
|
+
"arguments": [arg.to_mcp_dict() for arg in self.arguments],
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass(frozen=True, slots=True)
|
|
68
|
+
class McpResourceDefinition:
|
|
69
|
+
uri: str
|
|
70
|
+
name: str
|
|
71
|
+
description: str
|
|
72
|
+
mime_type: str
|
|
73
|
+
handler: ResourceHandler
|
|
74
|
+
|
|
75
|
+
def to_mcp_dict(self) -> dict[str, Any]:
|
|
76
|
+
return {
|
|
77
|
+
"uri": self.uri,
|
|
78
|
+
"name": self.name,
|
|
79
|
+
"description": self.description,
|
|
80
|
+
"mimeType": self.mime_type,
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _jsonrpc_result(request_id: Any, result: dict[str, Any]) -> dict[str, Any]:
|
|
85
|
+
return {"jsonrpc": "2.0", "id": request_id, "result": result}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _jsonrpc_error(request_id: Any, code: int, message: str) -> dict[str, Any]:
|
|
89
|
+
return {
|
|
90
|
+
"jsonrpc": "2.0",
|
|
91
|
+
"id": request_id,
|
|
92
|
+
"error": {"code": code, "message": message},
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class McpProtocolHandler:
|
|
97
|
+
"""纯标准库 MCP JSON-RPC 2.0 协议分发器(支持只读 tools、prompts、resources 与版本协商)."""
|
|
98
|
+
|
|
99
|
+
def __init__(
|
|
100
|
+
self,
|
|
101
|
+
tools: tuple[McpToolDefinition, ...],
|
|
102
|
+
*,
|
|
103
|
+
prompts: tuple[McpPromptDefinition, ...] = (),
|
|
104
|
+
resources: tuple[McpResourceDefinition, ...] = (),
|
|
105
|
+
server_name: str = "git-contribution-analyzer",
|
|
106
|
+
server_version: str = __version__,
|
|
107
|
+
) -> None:
|
|
108
|
+
self._server_name = server_name
|
|
109
|
+
self._server_version = server_version
|
|
110
|
+
self._tools: dict[str, McpToolDefinition] = {tool.name: tool for tool in tools}
|
|
111
|
+
self._ordered_tools = tools
|
|
112
|
+
self._prompts: dict[str, McpPromptDefinition] = {prompt.name: prompt for prompt in prompts}
|
|
113
|
+
self._ordered_prompts = prompts
|
|
114
|
+
self._resources: dict[str, McpResourceDefinition] = {res.uri: res for res in resources}
|
|
115
|
+
self._ordered_resources = resources
|
|
116
|
+
|
|
117
|
+
def handle_line(self, raw_line: str) -> dict[str, Any] | None:
|
|
118
|
+
text = raw_line.strip()
|
|
119
|
+
if not text:
|
|
120
|
+
return None
|
|
121
|
+
try:
|
|
122
|
+
message = json.loads(text)
|
|
123
|
+
except json.JSONDecodeError:
|
|
124
|
+
return _jsonrpc_error(None, JSONRPC_PARSE_ERROR, "Parse error: invalid JSON")
|
|
125
|
+
if not isinstance(message, dict):
|
|
126
|
+
return _jsonrpc_error(None, JSONRPC_INVALID_REQUEST, "Invalid Request: expected object")
|
|
127
|
+
return self.handle_message(message)
|
|
128
|
+
|
|
129
|
+
def handle_message(self, message: dict[str, Any]) -> dict[str, Any] | None:
|
|
130
|
+
if message.get("jsonrpc") != "2.0":
|
|
131
|
+
return _jsonrpc_error(
|
|
132
|
+
message.get("id"),
|
|
133
|
+
JSONRPC_INVALID_REQUEST,
|
|
134
|
+
"Invalid Request: jsonrpc must be '2.0'",
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
method = message.get("method")
|
|
138
|
+
if not isinstance(method, str) or not method:
|
|
139
|
+
return _jsonrpc_error(
|
|
140
|
+
message.get("id"),
|
|
141
|
+
JSONRPC_INVALID_REQUEST,
|
|
142
|
+
"Invalid Request: method must be a non-empty string",
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
has_id = "id" in message
|
|
146
|
+
request_id = message.get("id")
|
|
147
|
+
params = message.get("params")
|
|
148
|
+
if params is None:
|
|
149
|
+
params_dict: dict[str, Any] = {}
|
|
150
|
+
elif isinstance(params, dict):
|
|
151
|
+
params_dict = params
|
|
152
|
+
else:
|
|
153
|
+
if not has_id:
|
|
154
|
+
return None
|
|
155
|
+
return _jsonrpc_error(
|
|
156
|
+
request_id,
|
|
157
|
+
JSONRPC_INVALID_PARAMS,
|
|
158
|
+
"Invalid params: expected JSON object",
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
# Notifications do not receive JSON-RPC responses
|
|
162
|
+
if not has_id or method.startswith("notifications/"):
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
if method == "initialize":
|
|
166
|
+
return self._handle_initialize(request_id, params_dict)
|
|
167
|
+
if method == "ping":
|
|
168
|
+
return _jsonrpc_result(request_id, {})
|
|
169
|
+
if method == "tools/list":
|
|
170
|
+
return _jsonrpc_result(
|
|
171
|
+
request_id,
|
|
172
|
+
{"tools": [tool.to_mcp_dict() for tool in self._ordered_tools]},
|
|
173
|
+
)
|
|
174
|
+
if method == "tools/call":
|
|
175
|
+
return self._handle_tool_call(request_id, params_dict)
|
|
176
|
+
if method == "prompts/list":
|
|
177
|
+
return _jsonrpc_result(
|
|
178
|
+
request_id,
|
|
179
|
+
{"prompts": [prompt.to_mcp_dict() for prompt in self._ordered_prompts]},
|
|
180
|
+
)
|
|
181
|
+
if method == "prompts/get":
|
|
182
|
+
return self._handle_prompt_get(request_id, params_dict)
|
|
183
|
+
if method == "resources/list":
|
|
184
|
+
return _jsonrpc_result(
|
|
185
|
+
request_id,
|
|
186
|
+
{"resources": [res.to_mcp_dict() for res in self._ordered_resources]},
|
|
187
|
+
)
|
|
188
|
+
if method == "resources/read":
|
|
189
|
+
return self._handle_resource_read(request_id, params_dict)
|
|
190
|
+
|
|
191
|
+
return _jsonrpc_error(
|
|
192
|
+
request_id,
|
|
193
|
+
JSONRPC_METHOD_NOT_FOUND,
|
|
194
|
+
f"Method not found: {method}",
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
def _handle_initialize(
|
|
198
|
+
self,
|
|
199
|
+
request_id: Any,
|
|
200
|
+
params: dict[str, Any],
|
|
201
|
+
) -> dict[str, Any]:
|
|
202
|
+
requested = params.get("protocolVersion")
|
|
203
|
+
if isinstance(requested, str) and requested in SUPPORTED_PROTOCOL_VERSIONS:
|
|
204
|
+
negotiated = requested
|
|
205
|
+
else:
|
|
206
|
+
negotiated = SUPPORTED_PROTOCOL_VERSIONS[0]
|
|
207
|
+
capabilities: dict[str, Any] = {"tools": {"listChanged": False}}
|
|
208
|
+
if self._ordered_prompts:
|
|
209
|
+
capabilities["prompts"] = {"listChanged": False}
|
|
210
|
+
if self._ordered_resources:
|
|
211
|
+
capabilities["resources"] = {"subscribe": False, "listChanged": False}
|
|
212
|
+
return _jsonrpc_result(
|
|
213
|
+
request_id,
|
|
214
|
+
{
|
|
215
|
+
"protocolVersion": negotiated,
|
|
216
|
+
"capabilities": capabilities,
|
|
217
|
+
"serverInfo": {
|
|
218
|
+
"name": self._server_name,
|
|
219
|
+
"version": self._server_version,
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
def _handle_tool_call(
|
|
225
|
+
self,
|
|
226
|
+
request_id: Any,
|
|
227
|
+
params: dict[str, Any],
|
|
228
|
+
) -> dict[str, Any]:
|
|
229
|
+
name = params.get("name")
|
|
230
|
+
if not isinstance(name, str) or not name:
|
|
231
|
+
return _jsonrpc_error(
|
|
232
|
+
request_id,
|
|
233
|
+
JSONRPC_INVALID_PARAMS,
|
|
234
|
+
"Invalid params: tool 'name' must be a non-empty string",
|
|
235
|
+
)
|
|
236
|
+
tool = self._tools.get(name)
|
|
237
|
+
if tool is None:
|
|
238
|
+
return _jsonrpc_error(
|
|
239
|
+
request_id,
|
|
240
|
+
JSONRPC_METHOD_NOT_FOUND,
|
|
241
|
+
f"Unknown tool: {name}",
|
|
242
|
+
)
|
|
243
|
+
arguments = params.get("arguments", {})
|
|
244
|
+
if arguments is None:
|
|
245
|
+
arguments = {}
|
|
246
|
+
if not isinstance(arguments, dict):
|
|
247
|
+
return _jsonrpc_error(
|
|
248
|
+
request_id,
|
|
249
|
+
JSONRPC_INVALID_PARAMS,
|
|
250
|
+
"Invalid params: tool 'arguments' must be an object",
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
try:
|
|
254
|
+
payload = tool.handler(arguments)
|
|
255
|
+
except Exception as exc:
|
|
256
|
+
error_msg = str(exc) or exc.__class__.__name__
|
|
257
|
+
return _jsonrpc_result(
|
|
258
|
+
request_id,
|
|
259
|
+
{
|
|
260
|
+
"content": [{"type": "text", "text": error_msg}],
|
|
261
|
+
"isError": True,
|
|
262
|
+
},
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
rendered = json.dumps(payload, ensure_ascii=False, indent=2)
|
|
266
|
+
return _jsonrpc_result(
|
|
267
|
+
request_id,
|
|
268
|
+
{
|
|
269
|
+
"content": [{"type": "text", "text": rendered}],
|
|
270
|
+
"structuredContent": payload,
|
|
271
|
+
"isError": False,
|
|
272
|
+
},
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
def _handle_prompt_get(
|
|
276
|
+
self,
|
|
277
|
+
request_id: Any,
|
|
278
|
+
params: dict[str, Any],
|
|
279
|
+
) -> dict[str, Any]:
|
|
280
|
+
name = params.get("name")
|
|
281
|
+
if not isinstance(name, str) or not name:
|
|
282
|
+
return _jsonrpc_error(
|
|
283
|
+
request_id,
|
|
284
|
+
JSONRPC_INVALID_PARAMS,
|
|
285
|
+
"Invalid params: prompt 'name' must be a non-empty string",
|
|
286
|
+
)
|
|
287
|
+
prompt = self._prompts.get(name)
|
|
288
|
+
if prompt is None:
|
|
289
|
+
return _jsonrpc_error(
|
|
290
|
+
request_id,
|
|
291
|
+
JSONRPC_METHOD_NOT_FOUND,
|
|
292
|
+
f"Unknown prompt: {name}",
|
|
293
|
+
)
|
|
294
|
+
raw_args = params.get("arguments", {})
|
|
295
|
+
if raw_args is None:
|
|
296
|
+
raw_args = {}
|
|
297
|
+
if not isinstance(raw_args, dict):
|
|
298
|
+
return _jsonrpc_error(
|
|
299
|
+
request_id,
|
|
300
|
+
JSONRPC_INVALID_PARAMS,
|
|
301
|
+
"Invalid params: prompt 'arguments' must be an object",
|
|
302
|
+
)
|
|
303
|
+
str_args: dict[str, str] = {}
|
|
304
|
+
for key, value in raw_args.items():
|
|
305
|
+
if not isinstance(key, str) or not isinstance(value, str):
|
|
306
|
+
return _jsonrpc_error(
|
|
307
|
+
request_id,
|
|
308
|
+
JSONRPC_INVALID_PARAMS,
|
|
309
|
+
"Invalid params: prompt arguments must be string key/value pairs",
|
|
310
|
+
)
|
|
311
|
+
str_args[key] = value.strip()
|
|
312
|
+
|
|
313
|
+
for arg_spec in prompt.arguments:
|
|
314
|
+
if arg_spec.required and not str_args.get(arg_spec.name):
|
|
315
|
+
return _jsonrpc_error(
|
|
316
|
+
request_id,
|
|
317
|
+
JSONRPC_INVALID_PARAMS,
|
|
318
|
+
f"Missing required prompt argument: {arg_spec.name}",
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
messages = prompt.handler(str_args)
|
|
322
|
+
return _jsonrpc_result(
|
|
323
|
+
request_id,
|
|
324
|
+
{
|
|
325
|
+
"description": prompt.description,
|
|
326
|
+
"messages": messages,
|
|
327
|
+
},
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
def _handle_resource_read(
|
|
331
|
+
self,
|
|
332
|
+
request_id: Any,
|
|
333
|
+
params: dict[str, Any],
|
|
334
|
+
) -> dict[str, Any]:
|
|
335
|
+
uri = params.get("uri")
|
|
336
|
+
if not isinstance(uri, str) or not uri:
|
|
337
|
+
return _jsonrpc_error(
|
|
338
|
+
request_id,
|
|
339
|
+
JSONRPC_INVALID_PARAMS,
|
|
340
|
+
"Invalid params: resource 'uri' must be a non-empty string",
|
|
341
|
+
)
|
|
342
|
+
resource = self._resources.get(uri)
|
|
343
|
+
if resource is None:
|
|
344
|
+
return _jsonrpc_error(
|
|
345
|
+
request_id,
|
|
346
|
+
JSONRPC_METHOD_NOT_FOUND,
|
|
347
|
+
f"Unknown resource URI: {uri}",
|
|
348
|
+
)
|
|
349
|
+
text = resource.handler()
|
|
350
|
+
return _jsonrpc_result(
|
|
351
|
+
request_id,
|
|
352
|
+
{
|
|
353
|
+
"contents": [
|
|
354
|
+
{
|
|
355
|
+
"uri": resource.uri,
|
|
356
|
+
"mimeType": resource.mime_type,
|
|
357
|
+
"text": text,
|
|
358
|
+
}
|
|
359
|
+
]
|
|
360
|
+
},
|
|
361
|
+
)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import TextIO
|
|
8
|
+
|
|
9
|
+
from git_contribution_analyzer.adapters.mcp.prompts_resources import (
|
|
10
|
+
build_readonly_prompts,
|
|
11
|
+
build_readonly_resources,
|
|
12
|
+
)
|
|
13
|
+
from git_contribution_analyzer.adapters.mcp.protocol import McpProtocolHandler
|
|
14
|
+
from git_contribution_analyzer.adapters.mcp.tools import WorkspaceSandbox, build_readonly_tools
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _configure_utf8_stdio(in_stream: TextIO, out_stream: TextIO) -> None:
|
|
18
|
+
"""MCP stdio 协议要求 UTF-8 与 LF 分隔, 避免非 UTF-8 宿主机代码页 (如 cp1252) 崩溃."""
|
|
19
|
+
if isinstance(in_stream, io.TextIOWrapper):
|
|
20
|
+
in_stream.reconfigure(encoding="utf-8")
|
|
21
|
+
if isinstance(out_stream, io.TextIOWrapper):
|
|
22
|
+
out_stream.reconfigure(encoding="utf-8", newline="\n")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def run_stdio_server(
|
|
26
|
+
allowed_roots: tuple[Path, ...],
|
|
27
|
+
*,
|
|
28
|
+
stdin: TextIO | None = None,
|
|
29
|
+
stdout: TextIO | None = None,
|
|
30
|
+
) -> int:
|
|
31
|
+
"""运行纯标准库 newline-delimited JSON-RPC 2.0 stdio MCP Server."""
|
|
32
|
+
in_stream = stdin if stdin is not None else sys.stdin
|
|
33
|
+
out_stream = stdout if stdout is not None else sys.stdout
|
|
34
|
+
if stdin is None or stdout is None:
|
|
35
|
+
_configure_utf8_stdio(in_stream, out_stream)
|
|
36
|
+
|
|
37
|
+
sandbox = WorkspaceSandbox(allowed_roots)
|
|
38
|
+
handler = McpProtocolHandler(
|
|
39
|
+
build_readonly_tools(sandbox),
|
|
40
|
+
prompts=build_readonly_prompts(),
|
|
41
|
+
resources=build_readonly_resources(sandbox),
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
for raw_line in in_stream:
|
|
46
|
+
response = handler.handle_line(raw_line)
|
|
47
|
+
if response is not None:
|
|
48
|
+
out_stream.write(json.dumps(response, ensure_ascii=False) + "\n")
|
|
49
|
+
out_stream.flush()
|
|
50
|
+
except KeyboardInterrupt:
|
|
51
|
+
return 0
|
|
52
|
+
return 0
|