Skip to main content
Glama
l4b4r4b4b4
by l4b4r4b4b4

get_portfolio_metrics

Calculate comprehensive portfolio metrics including expected return, volatility, Sharpe ratio, and value at risk to evaluate portfolio performance.

Instructions

Get comprehensive metrics for a portfolio.

    Calculates and returns all key portfolio metrics including
    risk-adjusted returns, volatility measures, and risk metrics.

    Args:
        name: The portfolio name.

    Returns:
        Dictionary containing:
        - expected_return: Annualized expected return
        - volatility: Annualized volatility (standard deviation)
        - sharpe_ratio: Risk-adjusted return (Sharpe)
        - sortino_ratio: Downside risk-adjusted return (Sortino)
        - value_at_risk: VaR at 95% confidence
        - downside_risk: Target downside deviation
        - skewness: Skewness per stock
        - kurtosis: Kurtosis per stock
        - beta: Portfolio beta (if market index available)
        - treynor_ratio: Treynor ratio (if beta available)

    Example:
        ```
        metrics = get_portfolio_metrics(name="tech_stocks")
        print(f"Expected Return: {metrics['expected_return']:.2%}")
        print(f"Volatility: {metrics['volatility']:.2%}")
        print(f"Sharpe Ratio: {metrics['sharpe_ratio']:.2f}")
        ```
    

Caching Behavior:

  • Any input parameter can accept a ref_id from a previous tool call

  • Large results return ref_id + preview; use get_cached_result to paginate

  • All responses include ref_id for future reference

Preview Size: server default. Override per-call with get_cached_result(ref_id, max_size=...).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The handler function that executes the get_portfolio_metrics tool logic. It retrieves portfolio data from the store, formats skewness/kurtosis, and returns a dictionary with all portfolio metrics (expected_return, volatility, sharpe_ratio, sortino_ratio, value_at_risk, downside_risk, skewness, kurtosis, beta, treynor_ratio, settings).
    @mcp.tool
    @cache.cached(
        namespace="public",
        ttl=None,  # Deterministic - infinite TTL
    )
    def get_portfolio_metrics(name: str) -> dict[str, Any]:
        """Get comprehensive metrics for a portfolio.
    
        Calculates and returns all key portfolio metrics including
        risk-adjusted returns, volatility measures, and risk metrics.
    
        Args:
            name: The portfolio name.
    
        Returns:
            Dictionary containing:
            - expected_return: Annualized expected return
            - volatility: Annualized volatility (standard deviation)
            - sharpe_ratio: Risk-adjusted return (Sharpe)
            - sortino_ratio: Downside risk-adjusted return (Sortino)
            - value_at_risk: VaR at 95% confidence
            - downside_risk: Target downside deviation
            - skewness: Skewness per stock
            - kurtosis: Kurtosis per stock
            - beta: Portfolio beta (if market index available)
            - treynor_ratio: Treynor ratio (if beta available)
    
        Example:
            ```
            metrics = get_portfolio_metrics(name="tech_stocks")
            print(f"Expected Return: {metrics['expected_return']:.2%}")
            print(f"Volatility: {metrics['volatility']:.2%}")
            print(f"Sharpe Ratio: {metrics['sharpe_ratio']:.2f}")
            ```
        """
        data = store.get(name)
        if data is None:
            return {
                "error": f"Portfolio '{name}' not found",
                "suggestion": "Use list_portfolios() to see available portfolios",
            }
    
        metrics = data["metrics"]
    
        # Format skewness and kurtosis for readability
        skewness = {}
        kurtosis = {}
        if metrics.get("skew"):
            for key, value in metrics["skew"].items():
                # Handle both index-based and column-based keys
                if isinstance(value, dict):
                    skewness.update(value)
                else:
                    skewness[str(key)] = value
    
        if metrics.get("kurtosis"):
            for key, value in metrics["kurtosis"].items():
                if isinstance(value, dict):
                    kurtosis.update(value)
                else:
                    kurtosis[str(key)] = value
    
        return {
            "portfolio_name": name,
            "expected_return": metrics["expected_return"],
            "volatility": metrics["volatility"],
            "sharpe_ratio": metrics["sharpe"],
            "sortino_ratio": metrics["sortino"],
            "value_at_risk": metrics["var"],
            "downside_risk": metrics["downside_risk"],
            "skewness": skewness,
            "kurtosis": kurtosis,
            "beta": metrics.get("beta"),
            "treynor_ratio": metrics.get("treynor"),
            "settings": data["settings"],
        }
  • app/server.py:137-139 (registration)
    Registration call in server.py where register_analysis_tools is called, which internally registers get_portfolio_metrics via the @mcp.tool decorator.
    register_portfolio_tools(mcp, store)
    register_analysis_tools(mcp, store, cache)
    register_optimization_tools(mcp, store, cache)
  • Registration function that uses @mcp.tool decorator to register get_portfolio_metrics as an MCP tool with FastMCP, also applying @cache.cached with infinite TTL for deterministic caching.
    def register_analysis_tools(
        mcp: FastMCP, store: PortfolioStore, cache: RefCache
    ) -> None:
        """Register analysis tools with the FastMCP server.
    
        Args:
            mcp: The FastMCP server instance.
            store: The portfolio store for persistence.
            cache: The RefCache instance for caching large results.
        """
    
        @mcp.tool
        @cache.cached(
            namespace="public",
            ttl=None,  # Deterministic - infinite TTL
        )
        def get_portfolio_metrics(name: str) -> dict[str, Any]:
            """Get comprehensive metrics for a portfolio.
    
            Calculates and returns all key portfolio metrics including
            risk-adjusted returns, volatility measures, and risk metrics.
    
            Args:
                name: The portfolio name.
    
            Returns:
                Dictionary containing:
                - expected_return: Annualized expected return
                - volatility: Annualized volatility (standard deviation)
                - sharpe_ratio: Risk-adjusted return (Sharpe)
                - sortino_ratio: Downside risk-adjusted return (Sortino)
                - value_at_risk: VaR at 95% confidence
                - downside_risk: Target downside deviation
                - skewness: Skewness per stock
                - kurtosis: Kurtosis per stock
                - beta: Portfolio beta (if market index available)
                - treynor_ratio: Treynor ratio (if beta available)
    
            Example:
                ```
                metrics = get_portfolio_metrics(name="tech_stocks")
                print(f"Expected Return: {metrics['expected_return']:.2%}")
                print(f"Volatility: {metrics['volatility']:.2%}")
                print(f"Sharpe Ratio: {metrics['sharpe_ratio']:.2f}")
                ```
            """
            data = store.get(name)
            if data is None:
                return {
                    "error": f"Portfolio '{name}' not found",
                    "suggestion": "Use list_portfolios() to see available portfolios",
                }
    
            metrics = data["metrics"]
    
            # Format skewness and kurtosis for readability
            skewness = {}
            kurtosis = {}
            if metrics.get("skew"):
                for key, value in metrics["skew"].items():
                    # Handle both index-based and column-based keys
                    if isinstance(value, dict):
                        skewness.update(value)
                    else:
                        skewness[str(key)] = value
    
            if metrics.get("kurtosis"):
                for key, value in metrics["kurtosis"].items():
                    if isinstance(value, dict):
                        kurtosis.update(value)
                    else:
                        kurtosis[str(key)] = value
    
            return {
                "portfolio_name": name,
                "expected_return": metrics["expected_return"],
                "volatility": metrics["volatility"],
                "sharpe_ratio": metrics["sharpe"],
                "sortino_ratio": metrics["sortino"],
                "value_at_risk": metrics["var"],
                "downside_risk": metrics["downside_risk"],
                "skewness": skewness,
                "kurtosis": kurtosis,
                "beta": metrics.get("beta"),
                "treynor_ratio": metrics.get("treynor"),
                "settings": data["settings"],
            }
  • Schema: takes a single string parameter 'name' (the portfolio name) and returns a dictionary with typed fields (expected_return, volatility, sharpe_ratio, sortino_ratio, value_at_risk, downside_risk, skewness, kurtosis, beta, treynor_ratio, settings).
    def get_portfolio_metrics(name: str) -> dict[str, Any]:
        """Get comprehensive metrics for a portfolio.
    
        Calculates and returns all key portfolio metrics including
        risk-adjusted returns, volatility measures, and risk metrics.
    
        Args:
            name: The portfolio name.
    
        Returns:
            Dictionary containing:
            - expected_return: Annualized expected return
            - volatility: Annualized volatility (standard deviation)
            - sharpe_ratio: Risk-adjusted return (Sharpe)
            - sortino_ratio: Downside risk-adjusted return (Sortino)
            - value_at_risk: VaR at 95% confidence
            - downside_risk: Target downside deviation
            - skewness: Skewness per stock
            - kurtosis: Kurtosis per stock
            - beta: Portfolio beta (if market index available)
            - treynor_ratio: Treynor ratio (if beta available)
    
        Example:
            ```
            metrics = get_portfolio_metrics(name="tech_stocks")
            print(f"Expected Return: {metrics['expected_return']:.2%}")
            print(f"Volatility: {metrics['volatility']:.2%}")
            print(f"Sharpe Ratio: {metrics['sharpe_ratio']:.2f}")
            ```
        """
        data = store.get(name)
        if data is None:
            return {
                "error": f"Portfolio '{name}' not found",
                "suggestion": "Use list_portfolios() to see available portfolios",
            }
    
        metrics = data["metrics"]
    
        # Format skewness and kurtosis for readability
        skewness = {}
        kurtosis = {}
        if metrics.get("skew"):
            for key, value in metrics["skew"].items():
                # Handle both index-based and column-based keys
                if isinstance(value, dict):
                    skewness.update(value)
                else:
                    skewness[str(key)] = value
    
        if metrics.get("kurtosis"):
            for key, value in metrics["kurtosis"].items():
                if isinstance(value, dict):
                    kurtosis.update(value)
                else:
                    kurtosis[str(key)] = value
    
        return {
            "portfolio_name": name,
            "expected_return": metrics["expected_return"],
            "volatility": metrics["volatility"],
            "sharpe_ratio": metrics["sharpe"],
            "sortino_ratio": metrics["sortino"],
            "value_at_risk": metrics["var"],
            "downside_risk": metrics["downside_risk"],
            "skewness": skewness,
            "kurtosis": kurtosis,
            "beta": metrics.get("beta"),
            "treynor_ratio": metrics.get("treynor"),
            "settings": data["settings"],
        }
  • Test suite for get_portfolio_metrics covering expected fields, reasonable values, and error handling for nonexistent portfolios.
    class TestGetPortfolioMetrics:
        """Tests for get_portfolio_metrics tool."""
    
        def test_returns_all_expected_metrics(
            self,
            analysis_tools_with_cache: tuple[dict[str, Callable[..., Any]], Any],
            stored_analysis_portfolio: str,
        ) -> None:
            """Test that metrics include all expected fields."""
            tools, cache = analysis_tools_with_cache
            get_metrics = tools["get_portfolio_metrics"]
            raw_result = get_metrics(name=stored_analysis_portfolio)
            result = unwrap_cached(raw_result, cache=cache)
    
            assert "portfolio_name" in result
            assert result["portfolio_name"] == stored_analysis_portfolio
            assert "expected_return" in result
            assert "volatility" in result
            assert "sharpe_ratio" in result
            assert "sortino_ratio" in result
            assert "value_at_risk" in result
            assert "downside_risk" in result
            assert "skewness" in result
            assert "kurtosis" in result
            assert "settings" in result
    
        def test_metrics_have_reasonable_values(
            self,
            analysis_tools_with_cache: tuple[dict[str, Callable[..., Any]], Any],
            stored_analysis_portfolio: str,
        ) -> None:
            """Test that metrics are within reasonable bounds."""
            tools, cache = analysis_tools_with_cache
            get_metrics = tools["get_portfolio_metrics"]
            raw_result = get_metrics(name=stored_analysis_portfolio)
            result = unwrap_cached(raw_result, cache=cache)
    
            # Expected return should be a reasonable annual return
            assert -1.0 <= result["expected_return"] <= 5.0
    
            # Volatility should be positive
            assert result["volatility"] > 0
    
            # Sharpe ratio should be a reasonable value
            assert -10.0 <= result["sharpe_ratio"] <= 10.0
    
        def test_nonexistent_portfolio_returns_error(
            self,
            analysis_tools_with_cache: tuple[dict[str, Callable[..., Any]], RefCache],
        ) -> None:
            """Test that querying nonexistent portfolio returns error."""
            tools, cache = analysis_tools_with_cache
            get_metrics = tools["get_portfolio_metrics"]
            raw_result = get_metrics(name="nonexistent_portfolio")
            result = unwrap_cached(raw_result, cache)
    
            assert "error" in result
            assert "nonexistent_portfolio" in result["error"]
            assert "suggestion" in result
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Despite no annotations, the description reveals key behaviors: it calculates and returns a detailed dictionary with multiple metrics, mentions caching with ref_id and pagination via get_cached_result. However, it does not state that it is read-only or if any side effects exist.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with Args, Returns, and Example sections. The caching behavior block adds length but provides useful context. Could be slightly more concise, but it earns its place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (multiple return fields), the description is comprehensive: it lists all return keys, explains caching, and provides an example. The output schema exists but the description adds full context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The single parameter 'name' is thoroughly described as 'The portfolio name.' in the Args section, and the example provides context. This fully compensates for the lack of schema-level description (0% coverage).

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states 'Get comprehensive metrics for a portfolio.' with a specific verb and resource, and lists the included metrics. This distinguishes it from siblings like get_portfolio (basic info) and get_individual_stock_metrics (stock-level).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for obtaining full portfolio metrics but does not explicitly state when not to use it or provide comparisons with sibling tools. No guidance on prerequisites or exclusions, so it is only minimally adequate.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/l4b4r4b4b4/portfolio-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server