Skip to content
Last9
Book demo

OpenCode

Connect OpenCode to Last9 MCP to query logs, traces, metrics, and alerts from your coding session.

OpenCode is an open-source AI coding agent built for the terminal. It supports the Model Context Protocol (MCP), which means you can connect it to Last9’s MCP server and give the agent direct access to your production observability data — logs, traces, metrics, exceptions, and alerts — without leaving your terminal session.

Ask OpenCode questions like “Why is payment-service throwing 500s right now?” or “What are the slowest database queries in prod?” and the agent will query Last9 in real time to answer.

Prerequisites

  • OpenCode — installed and working (opencode --version)
  • Last9 account — with observability data flowing in via OpenTelemetry
  • Org slug: Find it in https://app.last9.io/v2/organizations/<org_slug>/....

Setup

To install the Last9 MCP connection and investigation skills together, use the Last9 OpenCode plugin. The plugin configures the hosted endpoint; you then authorize it with opencode mcp auth last9.

Follow the steps below if you prefer to configure the MCP connection directly.

  1. Locate or create your OpenCode config file

    Use opencode.json or opencode.jsonc in your project root. For global settings, use ~/.config/opencode/opencode.json. See OpenCode configuration locations.

  2. Add the Last9 MCP server

    Add Last9 as a remote MCP server using OAuth. Replace <org_slug> with your organization slug:

    {
    "$schema": "https://opencode.ai/config.json",
    "mcp": {
    "last9": {
    "type": "remote",
    "url": "https://app.last9.io/api/v4/organizations/<org_slug>/mcp",
    "enabled": true
    }
    }
    }

    Then authenticate:

    opencode mcp auth last9

    Sign in with your Last9 account in the browser window and authorize access.

  3. Verify the connection

    Start OpenCode and check that the Last9 tools are available:

    opencode

    Once inside a session, ask:

    What exceptions occurred in the last hour?

    OpenCode will invoke the Last9 MCP server and return results directly in your terminal.

Example Use Cases

Debug production exceptions

I'm seeing errors from user-service in prod. What's the root cause?

OpenCode can use get_exceptions and get_service_performance_details to inspect aggregated exception evidence and service performance.

Investigate slow endpoints

Which API endpoints are slowest in payment-service right now?

OpenCode calls get_service_operations_summary and prometheus_range_query to break down p95 latency by endpoint.

Correlate errors with deployments

We had a spike in errors around 3pm. Was there a deployment at that time?

OpenCode calls get_change_events to find deployments and correlates them with error rate data.

Surface slow database queries

What are the slowest PostgreSQL queries hitting orders-db in the last 30 minutes?

OpenCode calls get_database_slow_queries and get_database_queries to return query patterns ranked by duration.

Check active alerts

Are there any firing alerts right now?

OpenCode calls get_alerts to list currently active alerts with severity and firing timestamps.

Configuration reference

The mcp block in opencode.json accepts these fields per server:

FieldTypeDescription
type"remote" | "local"remote connects to a hosted MCP endpoint; local spawns a subprocess
urlstringRemote MCP endpoint URL (required when type is remote)
commandstring[]Command and arguments to start the local binary (required when type is local)
enabledbooleanSet to false to temporarily disable without removing the config
environmentobjectEnvironment variables passed to the local subprocess

Available tools

Once connected, OpenCode can use all Last9 tools. For the full list and parameters, see the Last9 MCP reference.


Troubleshooting

Last9 tools not showing up in OpenCode

  • Confirm "enabled": true in your opencode.json
  • Run opencode mcp auth last9 if using the remote type and you haven’t authenticated yet
  • Check that your org slug in the URL is correct (no trailing slash, no extra path segments)

OAuth flow fails or redirects to a 404

  • Log in to app.last9.io in your browser first, then re-run opencode mcp auth last9

“No data returned” for queries

  • Make sure your services are sending telemetry to Last9 via the OpenTelemetry integration
  • Try broadening the time range in your question (“last 2 hours” instead of “last 15 minutes”)

Local binary: authentication errors

  • Verify LAST9_REFRESH_TOKEN is set correctly in the environment block
  • Regenerate the token from API Access if it has expired

Please get in touch with us on Discord or Email if you have any questions.