aigroup-econ-mcp 0.4.0__py3-none-any.whl → 0.4.2__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.
- aigroup_econ_mcp/__init__.py +1 -1
- aigroup_econ_mcp/cli.py +81 -86
- aigroup_econ_mcp/server.py +1 -1
- aigroup_econ_mcp/tools/base.py +204 -5
- aigroup_econ_mcp/tools/file_parser.py +273 -4
- aigroup_econ_mcp/tools/machine_learning.py +56 -669
- aigroup_econ_mcp/tools/ml_ensemble.py +210 -0
- aigroup_econ_mcp/tools/ml_evaluation.py +272 -0
- aigroup_econ_mcp/tools/ml_models.py +54 -0
- aigroup_econ_mcp/tools/ml_regularization.py +172 -0
- aigroup_econ_mcp/tools/tool_registry.py +1 -1
- aigroup_econ_mcp-0.4.2.dist-info/METADATA +360 -0
- aigroup_econ_mcp-0.4.2.dist-info/RECORD +29 -0
- aigroup_econ_mcp/server_v1_backup.py +0 -1250
- aigroup_econ_mcp/server_v1_old.py +0 -1250
- aigroup_econ_mcp/server_with_file_support.py +0 -259
- aigroup_econ_mcp/tools/decorators.py +0 -178
- aigroup_econ_mcp/tools/file_input_handler.py +0 -268
- aigroup_econ_mcp-0.4.0.dist-info/METADATA +0 -718
- aigroup_econ_mcp-0.4.0.dist-info/RECORD +0 -30
- {aigroup_econ_mcp-0.4.0.dist-info → aigroup_econ_mcp-0.4.2.dist-info}/WHEEL +0 -0
- {aigroup_econ_mcp-0.4.0.dist-info → aigroup_econ_mcp-0.4.2.dist-info}/entry_points.txt +0 -0
- {aigroup_econ_mcp-0.4.0.dist-info → aigroup_econ_mcp-0.4.2.dist-info}/licenses/LICENSE +0 -0
aigroup_econ_mcp/__init__.py
CHANGED
aigroup_econ_mcp/cli.py
CHANGED
|
@@ -1,87 +1,82 @@
|
|
|
1
|
-
"""
|
|
2
|
-
AIGroup 计量经济学 MCP 服务命令行入口
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
import sys
|
|
6
|
-
import click
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@click.
|
|
12
|
-
@click.option('--
|
|
13
|
-
@click.option('--
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@click.option('--
|
|
18
|
-
@click.option('--
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
click.echo("
|
|
30
|
-
click.echo("
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
#
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
click.echo(f"[INFO]
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
click.echo(f"[INFO]
|
|
57
|
-
click.echo(f"[INFO]
|
|
58
|
-
click.echo(f"[INFO]
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
click.echo(f"[INFO]
|
|
72
|
-
click.echo(f"[INFO]
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
mount_path=mount_path or '/sse'
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if __name__ == "__main__":
|
|
1
|
+
"""
|
|
2
|
+
AIGroup 计量经济学 MCP 服务命令行入口
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import click
|
|
7
|
+
import uvicorn
|
|
8
|
+
from .server import create_mcp_server
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@click.command()
|
|
12
|
+
@click.option('--port', default=8000, help='服务器端口')
|
|
13
|
+
@click.option('--host', default='127.0.0.1', help='服务器地址')
|
|
14
|
+
@click.option('--transport', default='stdio',
|
|
15
|
+
type=click.Choice(['stdio', 'streamable-http', 'sse']),
|
|
16
|
+
help='传输协议 (默认: stdio)')
|
|
17
|
+
@click.option('--debug', is_flag=True, help='启用调试模式')
|
|
18
|
+
@click.option('--mount-path', default=None, help='挂载路径')
|
|
19
|
+
@click.option('--version', is_flag=True, help='显示版本信息')
|
|
20
|
+
def cli(port: int, host: str, transport: str, debug: bool, mount_path: str, version: bool):
|
|
21
|
+
"""AIGroup 计量经济学 MCP 工具
|
|
22
|
+
|
|
23
|
+
默认以stdio模式启动MCP服务器,适用于MCP客户端集成。
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
# 处理版本标志
|
|
27
|
+
if version:
|
|
28
|
+
from . import __version__
|
|
29
|
+
click.echo(f"aigroup-econ-mcp v{__version__}", err=True)
|
|
30
|
+
click.echo("Professional econometrics MCP tool", err=True)
|
|
31
|
+
click.echo("Author: AIGroup", err=True)
|
|
32
|
+
sys.exit(0)
|
|
33
|
+
|
|
34
|
+
# 创建MCP服务器
|
|
35
|
+
mcp_server = create_mcp_server()
|
|
36
|
+
|
|
37
|
+
# 设置调试模式
|
|
38
|
+
if debug:
|
|
39
|
+
mcp_server.settings.debug = True
|
|
40
|
+
click.echo(f"[DEBUG] 调试模式已启用", err=True)
|
|
41
|
+
|
|
42
|
+
# 根据传输协议启动服务器
|
|
43
|
+
if transport == 'stdio':
|
|
44
|
+
# stdio模式直接运行,不输出任何日志到stdout(MCP协议通信)
|
|
45
|
+
# 所有日志输出到stderr
|
|
46
|
+
from . import __version__
|
|
47
|
+
click.echo(f"[INFO] aigroup-econ-mcp v{__version__} starting...", err=True)
|
|
48
|
+
click.echo(f"[INFO] Transport: stdio (MCP protocol)", err=True)
|
|
49
|
+
if debug:
|
|
50
|
+
click.echo(f"[DEBUG] Debug mode enabled", err=True)
|
|
51
|
+
click.echo(f"[INFO] Server ready. Waiting for MCP client connection...", err=True)
|
|
52
|
+
mcp_server.run(transport='stdio')
|
|
53
|
+
|
|
54
|
+
elif transport == 'streamable-http':
|
|
55
|
+
# Streamable HTTP模式 - 使用uvicorn手动启动
|
|
56
|
+
click.echo(f"[INFO] Starting aigroup-econ-mcp server", err=True)
|
|
57
|
+
click.echo(f"[INFO] Professional econometrics MCP tool for AI data analysis", err=True)
|
|
58
|
+
click.echo(f"[INFO] Transport protocol: {transport}", err=True)
|
|
59
|
+
click.echo(f"[INFO] Service address: http://{host}:{port}", err=True)
|
|
60
|
+
if mount_path:
|
|
61
|
+
click.echo(f"[INFO] Mount path: {mount_path}", err=True)
|
|
62
|
+
|
|
63
|
+
# 获取Starlette应用并启动uvicorn服务器
|
|
64
|
+
app = mcp_server.streamable_http_app()
|
|
65
|
+
uvicorn.run(app, host=host, port=port, log_level="info")
|
|
66
|
+
|
|
67
|
+
elif transport == 'sse':
|
|
68
|
+
# SSE模式 - 使用uvicorn手动启动
|
|
69
|
+
click.echo(f"[INFO] Starting aigroup-econ-mcp server", err=True)
|
|
70
|
+
click.echo(f"[INFO] Professional econometrics MCP tool for AI data analysis", err=True)
|
|
71
|
+
click.echo(f"[INFO] Transport protocol: {transport}", err=True)
|
|
72
|
+
click.echo(f"[INFO] Service address: http://{host}:{port}", err=True)
|
|
73
|
+
if mount_path:
|
|
74
|
+
click.echo(f"[INFO] Mount path: {mount_path}", err=True)
|
|
75
|
+
|
|
76
|
+
# 获取Starlette应用并启动uvicorn服务器
|
|
77
|
+
app = mcp_server.sse_app()
|
|
78
|
+
uvicorn.run(app, host=host, port=port, log_level="info")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if __name__ == "__main__":
|
|
87
82
|
cli()
|
aigroup_econ_mcp/server.py
CHANGED
aigroup_econ_mcp/tools/base.py
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
"""
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
工具模块基类和装饰器
|
|
3
|
+
整合了工具基类、装饰器、错误处理和优化组件
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import functools
|
|
7
7
|
from typing import Any, Dict, List, Optional, Callable, Type
|
|
8
|
+
from functools import wraps
|
|
9
|
+
from mcp.server.session import ServerSession
|
|
10
|
+
from mcp.server.fastmcp import Context
|
|
11
|
+
from mcp.types import CallToolResult, TextContent
|
|
12
|
+
|
|
8
13
|
from .validation import ValidationError, validate_econometric_data, validate_model_parameters
|
|
9
14
|
from .cache import cache_result, cache_model, global_econometric_cache
|
|
10
15
|
from .monitoring import monitor_performance, track_progress, global_performance_monitor
|
|
16
|
+
from .file_parser import FileParser
|
|
11
17
|
from ..config import get_config, econometric_config
|
|
12
18
|
|
|
13
19
|
|
|
20
|
+
# ============================================================================
|
|
21
|
+
# 错误类定义
|
|
22
|
+
# ============================================================================
|
|
23
|
+
|
|
14
24
|
class EconometricToolError(Exception):
|
|
15
25
|
"""计量经济学工具错误基类"""
|
|
16
26
|
|
|
@@ -45,6 +55,177 @@ class ConfigurationError(EconometricToolError):
|
|
|
45
55
|
pass
|
|
46
56
|
|
|
47
57
|
|
|
58
|
+
# ============================================================================
|
|
59
|
+
# 装饰器函数
|
|
60
|
+
# ============================================================================
|
|
61
|
+
|
|
62
|
+
def with_file_input(tool_type: str):
|
|
63
|
+
"""
|
|
64
|
+
为工具函数添加文件输入支持的装饰器
|
|
65
|
+
|
|
66
|
+
支持两种输入方式:
|
|
67
|
+
1. file_path: CSV/JSON文件路径
|
|
68
|
+
2. file_content: 文件内容字符串
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
tool_type: 工具类型 ('single_var', 'multi_var_dict', 'regression', 'panel', 'time_series')
|
|
72
|
+
|
|
73
|
+
使用示例:
|
|
74
|
+
@with_file_input('regression')
|
|
75
|
+
async def my_tool(ctx, y_data=None, x_data=None, file_path=None, file_content=None, file_format='auto', **kwargs):
|
|
76
|
+
# 如果提供了file_path或file_content,数据会被自动填充
|
|
77
|
+
pass
|
|
78
|
+
"""
|
|
79
|
+
def decorator(func: Callable) -> Callable:
|
|
80
|
+
@wraps(func)
|
|
81
|
+
async def wrapper(*args, **kwargs):
|
|
82
|
+
# 提取上下文和文件参数
|
|
83
|
+
ctx = args[0] if args else kwargs.get('ctx')
|
|
84
|
+
file_path = kwargs.get('file_path')
|
|
85
|
+
file_content = kwargs.get('file_content')
|
|
86
|
+
file_format = kwargs.get('file_format', 'auto')
|
|
87
|
+
|
|
88
|
+
# 优先处理file_path
|
|
89
|
+
if file_path:
|
|
90
|
+
try:
|
|
91
|
+
await ctx.info(f"检测到文件路径输入: {file_path}")
|
|
92
|
+
|
|
93
|
+
# 从文件路径解析
|
|
94
|
+
parsed = FileParser.parse_file_path(file_path, file_format)
|
|
95
|
+
|
|
96
|
+
await ctx.info(
|
|
97
|
+
f"文件解析成功:{parsed['n_variables']}个变量,"
|
|
98
|
+
f"{parsed['n_observations']}个观测"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# 转换为工具格式
|
|
102
|
+
converted = FileParser.convert_to_tool_format(parsed, tool_type)
|
|
103
|
+
|
|
104
|
+
# 更新kwargs
|
|
105
|
+
kwargs.update(converted)
|
|
106
|
+
|
|
107
|
+
await ctx.info(f"数据已转换为{tool_type}格式")
|
|
108
|
+
|
|
109
|
+
except Exception as e:
|
|
110
|
+
await ctx.error(f"文件解析失败: {str(e)}")
|
|
111
|
+
return CallToolResult(
|
|
112
|
+
content=[TextContent(type="text", text=f"文件解析错误: {str(e)}")],
|
|
113
|
+
isError=True
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
# 如果没有file_path但有file_content,处理文件内容
|
|
117
|
+
elif file_content:
|
|
118
|
+
try:
|
|
119
|
+
await ctx.info("检测到文件内容输入,开始解析...")
|
|
120
|
+
|
|
121
|
+
# 解析文件内容
|
|
122
|
+
parsed = FileParser.parse_file_content(file_content, file_format)
|
|
123
|
+
|
|
124
|
+
await ctx.info(
|
|
125
|
+
f"文件解析成功:{parsed['n_variables']}个变量,"
|
|
126
|
+
f"{parsed['n_observations']}个观测"
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
# 转换为工具格式
|
|
130
|
+
converted = FileParser.convert_to_tool_format(parsed, tool_type)
|
|
131
|
+
|
|
132
|
+
# 更新kwargs
|
|
133
|
+
kwargs.update(converted)
|
|
134
|
+
|
|
135
|
+
await ctx.info(f"数据已转换为{tool_type}格式")
|
|
136
|
+
|
|
137
|
+
except Exception as e:
|
|
138
|
+
await ctx.error(f"文件解析失败: {str(e)}")
|
|
139
|
+
return CallToolResult(
|
|
140
|
+
content=[TextContent(type="text", text=f"文件解析错误: {str(e)}")],
|
|
141
|
+
isError=True
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# 调用原函数
|
|
145
|
+
return await func(*args, **kwargs)
|
|
146
|
+
|
|
147
|
+
return wrapper
|
|
148
|
+
return decorator
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def with_error_handling(func: Callable) -> Callable:
|
|
152
|
+
"""
|
|
153
|
+
为工具函数添加统一错误处理的装饰器
|
|
154
|
+
"""
|
|
155
|
+
@wraps(func)
|
|
156
|
+
async def wrapper(*args, **kwargs):
|
|
157
|
+
ctx = args[0] if args else kwargs.get('ctx')
|
|
158
|
+
tool_name = func.__name__
|
|
159
|
+
|
|
160
|
+
try:
|
|
161
|
+
return await func(*args, **kwargs)
|
|
162
|
+
except Exception as e:
|
|
163
|
+
await ctx.error(f"{tool_name}执行出错: {str(e)}")
|
|
164
|
+
return CallToolResult(
|
|
165
|
+
content=[TextContent(type="text", text=f"错误: {str(e)}")],
|
|
166
|
+
isError=True
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
return wrapper
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def with_logging(func: Callable) -> Callable:
|
|
173
|
+
"""
|
|
174
|
+
为工具函数添加日志记录的装饰器
|
|
175
|
+
"""
|
|
176
|
+
@wraps(func)
|
|
177
|
+
async def wrapper(*args, **kwargs):
|
|
178
|
+
ctx = args[0] if args else kwargs.get('ctx')
|
|
179
|
+
tool_name = func.__name__
|
|
180
|
+
|
|
181
|
+
await ctx.info(f"开始执行 {tool_name}")
|
|
182
|
+
result = await func(*args, **kwargs)
|
|
183
|
+
await ctx.info(f"{tool_name} 执行完成")
|
|
184
|
+
|
|
185
|
+
return result
|
|
186
|
+
|
|
187
|
+
return wrapper
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def with_file_support_decorator(
|
|
191
|
+
tool_type: str,
|
|
192
|
+
enable_error_handling: bool = True,
|
|
193
|
+
enable_logging: bool = True
|
|
194
|
+
):
|
|
195
|
+
"""
|
|
196
|
+
组合装饰器:为计量经济学工具添加文件支持和其他标准功能
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
tool_type: 工具类型
|
|
200
|
+
enable_error_handling: 是否启用错误处理
|
|
201
|
+
enable_logging: 是否启用日志记录
|
|
202
|
+
|
|
203
|
+
使用示例:
|
|
204
|
+
@with_file_support_decorator('regression')
|
|
205
|
+
async def ols_regression(ctx, y_data=None, x_data=None, **kwargs):
|
|
206
|
+
# 只需要编写核心业务逻辑
|
|
207
|
+
pass
|
|
208
|
+
"""
|
|
209
|
+
def decorator(func: Callable) -> Callable:
|
|
210
|
+
wrapped = func
|
|
211
|
+
|
|
212
|
+
if enable_error_handling:
|
|
213
|
+
wrapped = with_error_handling(wrapped)
|
|
214
|
+
|
|
215
|
+
wrapped = with_file_input(tool_type)(wrapped)
|
|
216
|
+
|
|
217
|
+
if enable_logging:
|
|
218
|
+
wrapped = with_logging(wrapped)
|
|
219
|
+
|
|
220
|
+
return wrapped
|
|
221
|
+
|
|
222
|
+
return decorator
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
# ============================================================================
|
|
226
|
+
# 工具基类
|
|
227
|
+
# ============================================================================
|
|
228
|
+
|
|
48
229
|
class EconometricTool:
|
|
49
230
|
"""
|
|
50
231
|
计量经济学工具基类
|
|
@@ -206,16 +387,27 @@ class EconometricTool:
|
|
|
206
387
|
return global_econometric_cache.result_cache.get_function_cache_stats(self.tool_name) or {}
|
|
207
388
|
|
|
208
389
|
|
|
390
|
+
# ============================================================================
|
|
209
391
|
# 便捷装饰器函数
|
|
210
|
-
|
|
392
|
+
# ============================================================================
|
|
393
|
+
|
|
394
|
+
def econometric_tool_with_optimization(tool_name: str = None):
|
|
211
395
|
"""
|
|
212
|
-
|
|
396
|
+
计量经济学工具装饰器(带优化)
|
|
397
|
+
|
|
398
|
+
应用缓存、性能监控和错误处理
|
|
213
399
|
|
|
214
400
|
Args:
|
|
215
401
|
tool_name: 工具名称
|
|
216
402
|
|
|
217
403
|
Returns:
|
|
218
404
|
Callable: 装饰器函数
|
|
405
|
+
|
|
406
|
+
使用示例:
|
|
407
|
+
@econometric_tool_with_optimization('my_analysis')
|
|
408
|
+
def my_analysis(data):
|
|
409
|
+
# 自动获得缓存、监控和错误处理
|
|
410
|
+
pass
|
|
219
411
|
"""
|
|
220
412
|
def decorator(func):
|
|
221
413
|
name = tool_name or func.__name__
|
|
@@ -261,11 +453,18 @@ def validate_input(data_type: str = "generic"):
|
|
|
261
453
|
|
|
262
454
|
# 导出主要类和函数
|
|
263
455
|
__all__ = [
|
|
456
|
+
# 错误类
|
|
264
457
|
"EconometricToolError",
|
|
265
458
|
"DataValidationError",
|
|
266
459
|
"ModelFittingError",
|
|
267
460
|
"ConfigurationError",
|
|
461
|
+
# 基类
|
|
268
462
|
"EconometricTool",
|
|
269
|
-
|
|
463
|
+
# 装饰器
|
|
464
|
+
"with_file_input",
|
|
465
|
+
"with_error_handling",
|
|
466
|
+
"with_logging",
|
|
467
|
+
"with_file_support_decorator",
|
|
468
|
+
"econometric_tool_with_optimization",
|
|
270
469
|
"validate_input"
|
|
271
470
|
]
|