-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
145 lines (111 loc) · 5.38 KB
/
server.py
File metadata and controls
145 lines (111 loc) · 5.38 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
#!/usr/bin/env python3.10
"""
LinguaLint - AI-powered project planning and analysis platform
Copyright (C) 2026 Jefferson Richards
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
"""
LinguaLint MCP Server
Model Context Protocol server that exposes LinguaLint risk factor extraction
as tools for MCP clients. Uses FastMCP for simple server implementation.
"""
import json
import tempfile
import smtplib
from pathlib import Path
from datetime import datetime
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from mcp.server.fastmcp import FastMCP
from src.nlp_processor import ModernNLPProcessor
from src.report_generator import generate_html_report
from src.responsibility_analyzer import analyze_responsibility
from src.responsibility_report_generator import generate_responsibility_reports
from src.pdf_generator import generate_comprehensive_pdf
# Initialize MCP server
mcp = FastMCP("LinguaLint Event Code Extractor")
# Global processor instance
processor = ModernNLPProcessor()
# Output directory for analysis
ANALYSIS_BASE_DIR = Path("./lingualint_analysis")
ANALYSIS_BASE_DIR.mkdir(exist_ok=True)
@mcp.tool()
def extract_risk_factors(text_content: str) -> str:
"""
Extract risk factors and generate interactive D3.js report from text content.
Args:
text_content: Input text to analyze for risk factors
Returns:
Summary of extraction results and path to generated HTML report
"""
if not text_content.strip():
return "Error: Empty text content provided"
try:
# Process text through NLP pipeline
results = processor.process_text(text_content, enrich_wikipedia=True)
# Generate unique filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Create timestamped analysis folder
analysis_dir = ANALYSIS_BASE_DIR / f"analysis_{timestamp}"
analysis_dir.mkdir(exist_ok=True)
json_file = analysis_dir / f"extraction_{timestamp}.json"
html_file = analysis_dir / f"report_{timestamp}.html"
# Save JSON results
with open(json_file, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=2, ensure_ascii=False)
# Generate HTML report
generate_html_report(str(json_file), str(html_file))
# Generate Responsibility Futures Analysis
responsibility_summary = ""
try:
# Analyze responsibility
responsibility_report = analyze_responsibility(str(json_file))
# Save responsibility analysis JSON
responsibility_json = json_file.with_name(f"extraction_{timestamp}_responsibility_analysis.json")
with open(responsibility_json, 'w', encoding='utf-8') as f:
json.dump(responsibility_report, f, indent=2, ensure_ascii=False)
# Generate responsibility visualizations and HTML report
responsibility_files = generate_responsibility_reports(str(responsibility_json))
# Add to summary
responsibility_summary = f"""
- Responsibility Analysis: COMPLETED
- Analyzed {responsibility_report['total_entities']} entities across {responsibility_report['total_events']} events
- Responsibility report: {responsibility_files.get('html_report', 'N/A')}
- Generated visualizations: {len([f for f in responsibility_files.values() if f])} files"""
except Exception as e:
responsibility_summary = f"\n- Responsibility Analysis: FAILED ({str(e)})"
# Generate Comprehensive PDF
pdf_summary = ""
try:
pdf_file = generate_comprehensive_pdf(timestamp, analysis_dir)
if pdf_file:
pdf_summary = f"\n- Comprehensive PDF: GENERATED ({pdf_file.name})"
else:
pdf_summary = f"\n- Comprehensive PDF: FAILED (see logs)"
except Exception as e:
pdf_summary = f"\n- Comprehensive PDF: FAILED ({str(e)})"
# Create summary
sentences_count = len(results.get('_source', {}).get('sentences', []))
phen_count = len(results.get('_source', {}).get('phen', []))
wiki_count = len(results.get('_source', {}).get('wiki', []))
summary = f"""Risk Factor Extraction Complete:
- Processed {sentences_count} sentences
- Identified {phen_count} key phenomena
- Enriched with {wiki_count} Wikipedia entries{responsibility_summary}{pdf_summary}
- Interactive report: {html_file.absolute()}
- Raw data: {json_file.absolute()}
Open the HTML file in your browser to view the interactive D3.js visualization."""
return summary
except Exception as e:
return f"Error during extraction: {str(e)}"
if __name__ == "__main__":
mcp.run()