File size: 14,504 Bytes
fc49c8c
 
cea2220
 
 
 
 
fc49c8c
 
cea2220
fc49c8c
cea2220
fc49c8c
cea2220
 
1c78117
fc49c8c
 
 
 
cea2220
fc49c8c
cea2220
 
fc49c8c
 
 
cea2220
 
fc49c8c
9b88b42
cea2220
 
 
 
9b88b42
 
 
 
 
 
 
 
cea2220
9b88b42
 
cea2220
 
9b88b42
 
cea2220
 
 
 
 
9b88b42
 
cea2220
9b88b42
 
 
 
 
 
cea2220
9b88b42
 
 
 
 
 
 
 
cea2220
9b88b42
 
cea2220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b88b42
 
cea2220
 
 
 
9b88b42
 
cea2220
 
 
 
9b88b42
 
 
 
 
cea2220
 
 
 
9b88b42
 
 
 
 
 
 
 
cea2220
9b88b42
 
cea2220
 
 
 
 
9b88b42
 
 
 
 
cea2220
9b88b42
 
 
 
 
 
 
 
cea2220
9b88b42
 
cea2220
 
 
 
 
 
9b88b42
 
 
 
cea2220
 
 
 
9b88b42
 
 
 
 
 
 
 
cea2220
151bd32
cea2220
 
 
9b88b42
 
cea2220
 
 
 
9b88b42
 
cea2220
 
 
 
9b88b42
 
cea2220
 
 
 
9b88b42
 
 
 
 
cea2220
 
 
 
9b88b42
 
 
 
 
 
 
 
cea2220
9b88b42
 
cea2220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b88b42
 
 
4cc5533
cea2220
 
 
 
4cc5533
 
 
 
 
 
 
 
cea2220
4cc5533
 
cea2220
 
 
 
 
 
 
 
 
 
 
 
 
4cc5533
 
 
cea2220
 
 
 
 
9f411df
 
cea2220
 
9f411df
 
cea2220
9f411df
cea2220
9f411df
 
cea2220
 
 
9f411df
cea2220
9f411df
 
 
d038452
cea2220
 
 
 
d038452
 
cea2220
d038452
 
 
cea2220
d038452
cea2220
d038452
 
cea2220
 
 
 
 
 
 
d038452
 
 
cea2220
 
 
 
 
 
d038452
 
 
cea2220
 
 
 
 
d038452
 
 
cea2220
 
 
 
 
 
 
d038452
 
 
 
 
9b88b42
fc49c8c
 
 
 
 
 
 
cea2220
fc49c8c
 
cea2220
 
fc49c8c
 
cea2220
fc49c8c
 
 
 
 
cea2220
fc49c8c
 
9b88b42
 
cea2220
9b88b42
fc49c8c
 
cea2220
fc49c8c
 
 
 
 
cea2220
fc49c8c
 
9b88b42
 
cea2220
9b88b42
fc49c8c
 
 
 
 
cea2220
fc49c8c
 
9b88b42
 
cea2220
9b88b42
fc49c8c
 
 
 
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""MCP Router for Portfolio Intelligence Platform.

Compatibility layer that routes to unified mcp_tools module.
This file maintains backwards compatibility with existing code that uses
mcp_router.call_*_mcp() methods.

All actual implementation is now in backend.mcp_tools module.
"""

import json
import logging
from typing import Any, Dict, List

from backend import mcp_tools
from backend.utils.serialisation import dumps_str

logger = logging.getLogger(__name__)


class MCPRouter:
    """Router for orchestrating MCP tool calls.

    This is a compatibility layer - all actual implementation
    is in backend.mcp_tools module with namespaced functions.
    """

    def __init__(self):
        """Initialise MCP router (compatibility layer)."""
        logger.info("Initialising MCP router (compatibility layer)")

    # Yahoo Finance MCP methods
    async def call_yahoo_finance_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call Yahoo Finance MCP tool (delegates to market_* functions).

        Args:
            tool: Tool name (get_quote, get_historical_data, get_fundamentals)
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing Yahoo Finance MCP call: {tool}")

        if tool == "get_quote":
            tickers = params.get("tickers", [])
            return await mcp_tools.market_get_quote(dumps_str(tickers))

        elif tool == "get_historical_data":
            return await mcp_tools.market_get_historical_data(
                ticker=params["ticker"],
                period=params.get("period", "1y"),
                interval=params.get("interval", "1d"),
            )

        elif tool == "get_fundamentals":
            return await mcp_tools.market_get_fundamentals(ticker=params["ticker"])

        else:
            raise ValueError(f"Unknown Yahoo Finance tool: {tool}")

    # FMP MCP methods
    async def call_fmp_mcp(self, tool: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """Call Financial Modeling Prep MCP tool (delegates to market_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing FMP MCP call: {tool}")

        if tool == "get_company_profile":
            return await mcp_tools.market_get_company_profile(ticker=params["ticker"])

        elif tool == "get_income_statement":
            return await mcp_tools.market_get_income_statement(
                ticker=params["ticker"],
                period=params.get("period", "annual"),
                limit=str(params.get("limit", 5)),
            )

        elif tool == "get_balance_sheet":
            return await mcp_tools.market_get_balance_sheet(
                ticker=params["ticker"],
                period=params.get("period", "annual"),
                limit=str(params.get("limit", 5)),
            )

        elif tool == "get_cash_flow_statement":
            return await mcp_tools.market_get_cash_flow_statement(
                ticker=params["ticker"],
                period=params.get("period", "annual"),
                limit=str(params.get("limit", 5)),
            )

        elif tool == "get_financial_ratios":
            return await mcp_tools.market_get_financial_ratios(
                ticker=params["ticker"],
                ttm=str(params.get("ttm", True)).lower(),
            )

        elif tool == "get_key_metrics":
            return await mcp_tools.market_get_key_metrics(
                ticker=params["ticker"],
                ttm=str(params.get("ttm", True)).lower(),
            )

        else:
            raise ValueError(f"Unknown FMP tool: {tool}")

    # Trading MCP methods
    async def call_trading_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call Trading MCP tool (delegates to technical_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing Trading MCP call: {tool}")

        if tool == "get_technical_indicators":
            return await mcp_tools.technical_get_indicators(
                ticker=params["ticker"],
                period=params.get("period", "3mo"),
            )

        else:
            raise ValueError(f"Unknown Trading MCP tool: {tool}")

    # FRED MCP methods
    async def call_fred_mcp(self, tool: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """Call FRED MCP tool (delegates to market_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing FRED MCP call: {tool}")

        if tool == "get_economic_series":
            return await mcp_tools.market_get_economic_series(
                series_id=params["series_id"],
                observation_start=params.get("observation_start"),
                observation_end=params.get("observation_end"),
            )

        else:
            raise ValueError(f"Unknown FRED tool: {tool}")

    # Portfolio Optimizer MCP methods
    async def call_portfolio_optimizer_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call Portfolio Optimizer MCP tool (delegates to portfolio_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing Portfolio Optimizer MCP call: {tool}")

        market_data = params.get("market_data", [])
        market_data_json = dumps_str(market_data)
        risk_tolerance = params.get("risk_tolerance", "moderate")

        if tool == "optimize_hrp":
            return await mcp_tools.portfolio_optimize_hrp(
                market_data_json=market_data_json,
                risk_tolerance=risk_tolerance,
            )

        elif tool == "optimize_black_litterman":
            return await mcp_tools.portfolio_optimize_black_litterman(
                market_data_json=market_data_json,
                risk_tolerance=risk_tolerance,
            )

        elif tool == "optimize_mean_variance":
            return await mcp_tools.portfolio_optimize_mean_variance(
                market_data_json=market_data_json,
                risk_tolerance=risk_tolerance,
            )

        else:
            raise ValueError(f"Unknown Portfolio Optimizer tool: {tool}")

    # Risk Analyzer MCP methods
    async def call_risk_analyzer_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call Risk Analyzer MCP tool (delegates to risk_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing Risk Analyzer MCP call: {tool}")

        if tool == "analyze_risk":
            portfolio = params.get("portfolio", [])
            benchmark = params.get("benchmark")

            return await mcp_tools.risk_analyze(
                portfolio_json=dumps_str(portfolio),
                portfolio_value=str(params.get("portfolio_value", 100000)),
                confidence_level=str(params.get("confidence_level", 0.95)),
                time_horizon=str(params.get("time_horizon", 1)),
                method=params.get("method", "historical"),
                num_simulations=str(params.get("num_simulations", 10000)),
                benchmark_json=dumps_str(benchmark) if benchmark else None,
            )

        elif tool == "forecast_volatility_garch":
            returns = params.get("returns", [])

            return await mcp_tools.risk_forecast_volatility_garch(
                ticker=params["ticker"],
                returns_json=dumps_str([float(r) for r in returns]),
                forecast_horizon=str(params.get("forecast_horizon", 30)),
                garch_p=str(params.get("garch_p", 1)),
                garch_q=str(params.get("garch_q", 1)),
            )

        else:
            raise ValueError(f"Unknown Risk Analyzer tool: {tool}")

    # Ensemble Predictor MCP methods
    async def call_ensemble_predictor_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call Ensemble Predictor MCP tool (delegates to ml_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing Ensemble Predictor MCP call: {tool}")

        if tool == "forecast_ensemble":
            prices = params.get("prices", [])
            dates = params.get("dates")

            return await mcp_tools.ml_forecast_ensemble(
                ticker=params["ticker"],
                prices_json=dumps_str([float(p) for p in prices]),
                dates_json=dumps_str(dates) if dates else None,
                forecast_horizon=str(params.get("forecast_horizon", 30)),
                confidence_level=str(params.get("confidence_level", 0.95)),
                use_returns=str(params.get("use_returns", True)).lower(),
                ensemble_method=params.get("ensemble_method", "mean"),
            )

        else:
            raise ValueError(f"Unknown Ensemble Predictor tool: {tool}")

    # News Sentiment MCP methods
    async def call_news_sentiment_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call News Sentiment MCP tool (delegates to sentiment_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing News Sentiment MCP call: {tool}")

        if tool == "get_news_with_sentiment":
            return await mcp_tools.sentiment_get_news(
                ticker=params["ticker"],
                days_back=str(params.get("days_back", 7)),
            )

        else:
            raise ValueError(f"Unknown News Sentiment tool: {tool}")

    # Feature Extraction MCP methods
    async def call_feature_extraction_mcp(
        self, tool: str, params: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Call Feature Extraction MCP tool (delegates to technical_* functions).

        Args:
            tool: Tool name
            params: Tool parameters

        Returns:
            Tool result
        """
        logger.debug(f"Routing Feature Extraction MCP call: {tool}")

        if tool == "extract_technical_features":
            return await mcp_tools.technical_extract_features(
                ticker=params["ticker"],
                prices=dumps_str(params.get("prices", [])),
                volumes=dumps_str(params.get("volumes", [])),
                include_momentum=str(params.get("include_momentum", True)).lower(),
                include_volatility=str(params.get("include_volatility", True)).lower(),
                include_trend=str(params.get("include_trend", True)).lower(),
            )

        elif tool == "normalise_features":
            return await mcp_tools.technical_normalise_features(
                ticker=params["ticker"],
                features=dumps_str(params.get("features", {})),
                historical_features=dumps_str(params.get("historical_features", [])),
                window_size=str(params.get("window_size", 100)),
                method=params.get("method", "ewm"),
            )

        elif tool == "select_features":
            return await mcp_tools.technical_select_features(
                ticker=params["ticker"],
                feature_vector=dumps_str(params.get("feature_vector", {})),
                max_features=str(params.get("max_features", 15)),
                variance_threshold=str(params.get("variance_threshold", 0.95)),
            )

        elif tool == "compute_feature_vector":
            return await mcp_tools.technical_compute_feature_vector(
                ticker=params["ticker"],
                technical_features=dumps_str(params.get("technical_features", {})),
                fundamental_features=dumps_str(params.get("fundamental_features", {})),
                sentiment_features=dumps_str(params.get("sentiment_features", {})),
                max_features=str(params.get("max_features", 30)),
                selection_method=params.get("selection_method", "pca"),
            )

        else:
            raise ValueError(f"Unknown Feature Extraction tool: {tool}")

    # High-level helper methods
    async def fetch_market_data(self, tickers: List[str]) -> Dict[str, Any]:
        """Fetch market data for given tickers.

        Args:
            tickers: List of stock/asset tickers

        Returns:
            Market data from Yahoo Finance
        """
        logger.info(f"Fetching market data for {len(tickers)} tickers")
        results = await mcp_tools.market_get_quote(dumps_str(tickers))
        return {r.get("ticker", r.get("symbol")): r for r in results}

    async def fetch_fundamentals(self, tickers: List[str]) -> Dict[str, Any]:
        """Fetch fundamental data for tickers.

        Args:
            tickers: List of stock tickers

        Returns:
            Fundamental data per ticker
        """
        logger.info(f"Fetching fundamentals for {len(tickers)} tickers")
        results = {}
        for ticker in tickers:
            results[ticker] = await mcp_tools.market_get_company_profile(ticker)
        return results

    async def fetch_technical_indicators(self, tickers: List[str]) -> Dict[str, Any]:
        """Fetch technical indicators for tickers.

        Args:
            tickers: List of stock tickers

        Returns:
            Technical indicators per ticker
        """
        logger.info(f"Fetching technical indicators for {len(tickers)} tickers")
        results = {}
        for ticker in tickers:
            results[ticker] = await mcp_tools.technical_get_indicators(ticker)
        return results

    async def fetch_macro_data(self) -> Dict[str, Any]:
        """Fetch macroeconomic data from FRED.

        Returns:
            Macroeconomic indicators
        """
        logger.info("Fetching macroeconomic data")
        results = {}
        for series_id in ["GDP", "UNRATE", "DFF"]:
            results[series_id] = await mcp_tools.market_get_economic_series(series_id)
        return results


# Global MCP router instance
mcp_router = MCPRouter()