Skip to content

MCP factor_analysis always fails with KeyError: 'factor_csv' — wrapper and registered tool have mismatched contracts #635

Description

Summary

The MCP server's factor_analysis tool cannot succeed for any input: the
@mcp.tool wrapper in agent/mcp_server.py forwards codes/factor_name
arguments, but the only registered implementation
(agent/src/tools/factor_analysis_tool.py) requires pre-built CSV artifact
paths (factor_csv, return_csv, output_dir). Every MCP call therefore
dies on KeyError: 'factor_csv' before any analysis runs.

Reproduction

Connect any MCP client to vibe-trading-mcp (tested over Streamable HTTP)
and call:

{
  "tool": "factor_analysis",
  "arguments": {
    "codes": ["SBIN.NS", "HDFCBANK.NS", "ICICIBANK.NS"],
    "factor_name": "pb",
    "start_date": "2025-07-16",
    "end_date": "2026-07-16",
    "source": "yfinance"
  }
}

Result (same for any market/symbols, e.g. US tickers, and for source: "auto" / "tushare"):

factor_analysis error: 'factor_csv'

Root cause

agent/mcp_server.py — the wrapper accepts a codes-based signature and
forwards exactly those keys:

@mcp.tool
def factor_analysis(
    codes: list[str],
    factor_name: str,
    start_date: str,
    end_date: str,
    source: str = "auto",
    top_n: int = 10,
    bottom_n: int = 10,
) -> str:
    ...
    return registry.execute(
        "factor_analysis",
        {
            "codes": codes,
            "factor_name": factor_name,
            "start_date": start_date,
            "end_date": end_date,
            "source": source,
            "top_n": top_n,
            "bottom_n": bottom_n,
        },
    )

agent/src/tools/factor_analysis_tool.py — the registered tool is
artifact-based and reads different keys:

"required": ["factor_csv", "return_csv", "output_dir"],
...
factor_csv=kwargs["factor_csv"],   # <- KeyError: 'factor_csv'

FactorAnalysisTool performs no data fetching (it only pd.read_csvs the
two matrices), and I could not find any other code path that builds a factor
matrix from codes + factor_name (the wrapper's docstring mentions
daily_basic fields like pe_ttm/pb, which suggests an intended
tushare-backed builder that isn't wired up).

Verified at commit 86f6012 and, by reading the current sources, still
present on main as of 2026-07-16.

Environment

  • vibe-trading-ai installed from git @ 86f6012 (pip, Python 3.12, macOS arm64)
  • vibe-trading-mcp --transport http (Streamable HTTP), stock configuration
  • No broker connections, no mandate; pure research usage

Expected behavior (either would resolve it)

  1. The MCP wrapper matches the registered tool's contract — i.e. it accepts
    factor_csv/return_csv/output_dir so agents can feed artifacts they
    built with get_market_data + write_file (the flow the
    factor-research skill documents); or
  2. A codes→factor-matrix builder is added behind the current wrapper
    signature (e.g. tushare daily_basic for CN, with an honest error for
    markets lacking a fundamentals history source).

Happy to provide more detail or test a fix. Thanks for the project — the
MCP server design (research-only surface, no order tools) is excellent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions