Skip to main content
Glama
harshil1502

tradingview-mcp

by harshil1502

chart_get_state

Retrieve the current chart state including symbol, timeframe, visible studies, and last price from TradingView Desktop.

Instructions

Read the current chart state: symbol, timeframe, visible studies, and last price.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The chartGetState handler function that executes the tool logic. It takes an empty input and a TradingViewPage instance, delegates to page.getChartState(), and wraps errors in ToolExecutionError.
    export async function chartGetState(
      _input: z.infer<typeof chartGetStateInput>,
      page: TradingViewPage,
    ): Promise<z.infer<typeof chartGetStateOutput>> {
      try {
        return await page.getChartState();
      } catch (cause) {
        throw new ToolExecutionError(
          'chart_get_state',
          'Failed to read chart state',
          cause,
        );
      }
    }
  • Input schema (empty object, strict) and output schema (symbol, timeframe, studies, lastPrice) for chart_get_state.
    export const chartGetStateInput = z.object({}).strict();
    export const chartGetStateOutput = z.object({
      symbol: z.string(),
      timeframe: TimeframeSchema,
      studies: z.array(z.string()),
      lastPrice: z.number().nullable(),
    });
  • Registration of chart_get_state in the TOOLS array with name, description, input/output schemas, and handler function.
    export const TOOLS: ToolDef<any, any>[] = [
      // --- chart ---
      {
        name: 'chart_get_state',
        description:
          'Read the current chart state: symbol, timeframe, visible studies, and last price.',
        input: chartGetStateInput,
        output: chartGetStateOutput,
        handler: chartGetState,
      },
  • Import of chartGetState, chartGetStateInput, and chartGetStateOutput from chart.ts, used in the tool registration.
    chartGetState,
    chartGetStateInput,
    chartGetStateOutput,
  • The getChartState() method on TradingViewPage class that actually executes the logic via CDP: evaluates JavaScript in the browser to read TV widget state (symbol, resolution, studies, lastPrice) and maps the resolution to a canonical timeframe.
    async getChartState(): Promise<ChartState> {
      const expr = `
        (() => {
          const w = window.tvWidget || window.TradingView?.widget;
          if (!w || !w.activeChart) {
            return { error: 'tvWidget not available — is the chart loaded?' };
          }
          const chart = w.activeChart();
          const symbol = chart.symbol();
          const resolution = chart.resolution();
          const studies = chart.getAllStudies?.()?.map(s => s.name) ?? [];
          let lastPrice = null;
          try {
            const series = chart.getSeries?.();
            lastPrice = series?.lastPrice?.() ?? null;
          } catch (_) { /* ignore */ }
          return { symbol, resolution, studies, lastPrice };
        })()
      `;
      const raw = await this.cdp.evaluate<{
        error?: string;
        symbol?: string;
        resolution?: string;
        studies?: string[];
        lastPrice?: number | null;
      }>(expr);
    
      if (raw.error) {
        throw new ChartStateError(raw.error);
      }
    
      const tf = TV_TO_TIMEFRAME[raw.resolution ?? ''];
      if (!tf) {
        throw new ChartStateError(
          `Unknown TradingView resolution "${raw.resolution}" — add a mapping.`,
        );
      }
    
      return {
        symbol: raw.symbol ?? '',
        timeframe: tf,
        studies: raw.studies ?? [],
        lastPrice: raw.lastPrice ?? null,
      };
    }
Behavior4/5

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

Description explicitly declares the tool as read-only ('Read'), which aligns with the operation. It discloses the output fields. Since no annotations are provided, the description carries the full burden and does so adequately, though it could mention if auth or rate limits apply.

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

Conciseness5/5

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

Single sentence with no unnecessary words. Every part adds value: action, resource, and output details. Highly efficient.

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 no output schema, the description clearly lists all return values (symbol, timeframe, visible studies, last price). The tool is simple, and the description covers its purpose completely.

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

Parameters4/5

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

No parameters exist, and schema coverage is 100%. The description doesn't add parameter information, which is acceptable since there are none. Baseline score of 4 applies.

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?

Description clearly states the action (Read) and the resource (current chart state), and lists specific outputs (symbol, timeframe, visible studies, last price). It distinguishes itself from sibling tools like chart_get_ohlcv (which returns historical data) and chart_set_symbol (which modifies state).

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

Usage Guidelines2/5

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

No guidance on when to use this tool versus alternatives. It simply describes the function without providing context on scenarios or exclusions. For example, it doesn't indicate that for historical data one should use chart_get_ohlcv.

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/harshil1502/tradingview-mcp'

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