File size: 10,788 Bytes
32dc112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""

MissionControlMCP - Enterprise Automation MCP Server

Main server implementation using MCP SDK

"""
import logging
from typing import Any
import sys
import os

# Setup paths
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Import MCP SDK
from mcp.server import Server
from mcp.types import Tool, TextContent

# Import tool functions
from tools.pdf_reader import read_pdf
from tools.text_extractor import extract_text
from tools.web_fetcher import fetch_web_content
from tools.rag_search import search_documents
from tools.data_visualizer import visualize_data
from tools.file_converter import convert_file
from tools.email_intent_classifier import classify_email_intent
from tools.kpi_generator import generate_kpis

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Create MCP server instance
app = Server("mission-control-mcp")


# Tool definitions
TOOLS = [
    Tool(
        name="pdf_reader",
        description="Extract text and metadata from PDF files. Reads all pages and extracts document information.",
        inputSchema={
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Path to the PDF file to read"
                }
            },
            "required": ["file_path"]
        }
    ),
    Tool(
        name="text_extractor",
        description="Process and extract information from text. Supports cleaning, summarization, chunking, and keyword extraction.",
        inputSchema={
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Raw text to process"
                },
                "operation": {
                    "type": "string",
                    "description": "Operation: 'clean', 'summarize', 'chunk', or 'keywords'",
                    "enum": ["clean", "summarize", "chunk", "keywords"],
                    "default": "clean"
                },
                "max_length": {
                    "type": "integer",
                    "description": "Maximum length for summary or chunk size",
                    "default": 500
                }
            },
            "required": ["text"]
        }
    ),
    Tool(
        name="web_fetcher",
        description="Fetch and extract content from web URLs. Returns clean text or HTML content with metadata.",
        inputSchema={
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "description": "URL to fetch content from"
                },
                "extract_text_only": {
                    "type": "boolean",
                    "description": "Extract only text content (removes HTML)",
                    "default": True
                }
            },
            "required": ["url"]
        }
    ),
    Tool(
        name="rag_search",
        description="Semantic search using RAG (Retrieval Augmented Generation). Finds relevant documents using vector embeddings.",
        inputSchema={
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "Search query"
                },
                "documents": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "List of documents to search in"
                },
                "top_k": {
                    "type": "integer",
                    "description": "Number of top results to return",
                    "default": 3
                }
            },
            "required": ["query", "documents"]
        }
    ),
    Tool(
        name="data_visualizer",
        description="Create data visualizations and charts. Supports bar, line, pie, and scatter charts from JSON or CSV data.",
        inputSchema={
            "type": "object",
            "properties": {
                "data": {
                    "type": "string",
                    "description": "JSON or CSV string data"
                },
                "chart_type": {
                    "type": "string",
                    "description": "Chart type",
                    "enum": ["bar", "line", "pie", "scatter"],
                    "default": "bar"
                },
                "x_column": {
                    "type": "string",
                    "description": "X-axis column name"
                },
                "y_column": {
                    "type": "string",
                    "description": "Y-axis column name"
                },
                "title": {
                    "type": "string",
                    "description": "Chart title",
                    "default": "Data Visualization"
                }
            },
            "required": ["data"]
        }
    ),
    Tool(
        name="file_converter",
        description="Convert files between formats. Supports PDF↔TXT, TXT↔CSV conversions.",
        inputSchema={
            "type": "object",
            "properties": {
                "input_path": {
                    "type": "string",
                    "description": "Path to input file"
                },
                "output_format": {
                    "type": "string",
                    "description": "Desired output format",
                    "enum": ["txt", "csv", "pdf"]
                },
                "output_path": {
                    "type": "string",
                    "description": "Optional output file path"
                }
            },
            "required": ["input_path", "output_format"]
        }
    ),
    Tool(
        name="email_intent_classifier",
        description="Classify email intent using NLP. Identifies inquiry, complaint, request, feedback, meeting, order, urgent, follow-up, thank you, and application intents.",
        inputSchema={
            "type": "object",
            "properties": {
                "email_text": {
                    "type": "string",
                    "description": "Email text to classify"
                }
            },
            "required": ["email_text"]
        }
    ),
    Tool(
        name="kpi_generator",
        description="Generate business KPIs and insights from data. Calculates revenue, growth, efficiency, customer, and operational metrics.",
        inputSchema={
            "type": "object",
            "properties": {
                "data": {
                    "type": "string",
                    "description": "JSON string with business data"
                },
                "metrics": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "enum": ["revenue", "growth", "efficiency", "customer", "operational"]
                    },
                    "description": "List of metrics to calculate",
                    "default": ["revenue", "growth", "efficiency"]
                }
            },
            "required": ["data"]
        }
    )
]


@app.list_tools()
async def list_tools() -> list[Tool]:
    """List all available tools"""
    return TOOLS


@app.call_tool()
async def call_tool(name: str, arguments: Any) -> list[TextContent]:
    """

    Handle tool execution requests

    

    Args:

        name: Tool name

        arguments: Tool arguments

        

    Returns:

        List of TextContent responses

    """
    try:
        logger.info(f"Executing tool: {name}")
        
        result = None
        
        if name == "pdf_reader":
            result = read_pdf(arguments["file_path"])
            
        elif name == "text_extractor":
            result = extract_text(
                text=arguments["text"],
                operation=arguments.get("operation", "clean"),
                max_length=arguments.get("max_length", 500)
            )
            
        elif name == "web_fetcher":
            result = fetch_web_content(
                url=arguments["url"],
                extract_text_only=arguments.get("extract_text_only", True)
            )
            
        elif name == "rag_search":
            result = search_documents(
                query=arguments["query"],
                documents=arguments["documents"],
                top_k=arguments.get("top_k", 3)
            )
            
        elif name == "data_visualizer":
            result = visualize_data(
                data=arguments["data"],
                chart_type=arguments.get("chart_type", "bar"),
                x_column=arguments.get("x_column"),
                y_column=arguments.get("y_column"),
                title=arguments.get("title", "Data Visualization")
            )
            
        elif name == "file_converter":
            result = convert_file(
                input_path=arguments["input_path"],
                output_format=arguments["output_format"],
                output_path=arguments.get("output_path")
            )
            
        elif name == "email_intent_classifier":
            result = classify_email_intent(arguments["email_text"])
            
        elif name == "kpi_generator":
            result = generate_kpis(
                data=arguments["data"],
                metrics=arguments.get("metrics", ["revenue", "growth", "efficiency"])
            )
            
        else:
            raise ValueError(f"Unknown tool: {name}")
        
        # Format result as JSON string
        import json
        result_text = json.dumps(result, indent=2, default=str)
        
        return [TextContent(type="text", text=result_text)]
        
    except Exception as e:
        logger.error(f"Error executing tool {name}: {e}", exc_info=True)
        error_msg = f"Error executing {name}: {str(e)}"
        return [TextContent(type="text", text=error_msg)]


async def main():
    """Main entry point for the MCP server"""
    from mcp.server.stdio import stdio_server
    
    async with stdio_server() as (read_stream, write_stream):
        logger.info("MissionControlMCP server starting...")
        await app.run(
            read_stream,
            write_stream,
            app.create_initialization_options()
        )


if __name__ == "__main__":
    import asyncio
    asyncio.run(main())