Skip to main content
Glama
MasonChow

Source Map Parser MCP Server

parse_stack

Map JavaScript error stack traces to original source code locations using source maps, providing context lines for debugging.

Instructions

Parse Error Stack Trace

This tool allows you to parse error stack traces by providing the following:

  • A downloadable source map URL.

  • The line and column numbers from the stack trace.

The tool will map the provided stack trace information to the corresponding source code location using the source map. It also supports fetching additional context lines around the error location for better debugging.

Parameters:

  • stacks: An array of stack trace objects, each containing:

    • line: The line number in the stack trace.

    • column: The column number in the stack trace.

    • sourceMapUrl: The URL of the source map file corresponding to the stack trace.

  • ctxOffset (optional): The number of additional context lines to include before and after the error location in the source code. Defaults to 5.

Returns:

  • A JSON object containing the parsed stack trace information, including the mapped source code location and context lines.

  • If parsing fails, an error message will be returned for the corresponding stack trace.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
stacksYes

Implementation Reference

  • Core handler logic for parsing multiple stack traces: fetches unique source maps, generates tokens for each stack entry using getSourceToken, handles errors per entry.
    public async batchParseStack(stackArr: Stack[]): Promise<BatchParseResult> {
      if (!stackArr.length) return [];
    
      // Ensure initialization is complete
      await this.init();
    
      const result: BatchParseResult = [];
    
      // Step 1: Get all necessary source map contents, eliminating duplicates
      const uniqueUrls = [...new Set(stackArr.map(stack => stack.sourceMapUrl))];
      const sourceMapMap = new Map<string, string>();
      const sourceMapErrors = new Map<string, Error>();
      // Fetch all unique source maps in parallel
      await Promise.all(uniqueUrls.map(async (url) => {
        try {
          const content = await this.fetchSourceMapContent(url);
          sourceMapMap.set(url, content);
        } catch (error) {
          sourceMapErrors.set(url, error instanceof Error
            ? error
            : new Error("fetch source map error: " + error));
        }
      }));
    
      // Step 2: Generate tokens using the fetched source map contents
      await Promise.all(stackArr.map(async (stack, idx) => {
        // If source map fetch failed, return an error directly
        if (sourceMapErrors.has(stack.sourceMapUrl)) {
          result[idx] = {
            success: false,
            error: new Error("parse token error: source map fetch failed", {
              cause: sourceMapErrors.get(stack.sourceMapUrl)
            })
          };
          return;
        }
    
        // Use the fetched source map content
        const sourceMapContent = sourceMapMap.get(stack.sourceMapUrl);
        if (!sourceMapContent) {
          result[idx] = {
            success: false,
            error: new Error("parse token error: source map content not found")
          };
          return;
        }
    
        try {
          // Use the dedicated method to get the token
          const token = await this.getSourceToken(stack.line, stack.column, sourceMapContent);
          result[idx] = {
            success: true,
            token,
          };
        } catch (error) {
          result[idx] = {
            success: false,
            error: new Error("parse token error: " + (error instanceof Error ? error.message : error), {
              cause: error,
            })
          };
        }
      }));
    
      return result;
    }
  • MCP tool handler wrapper for parse_stack: validates params, calls Parser.batchParseStack, formats and sanitizes response for MCP protocol.
    handler: async ({ stacks }, getParser) => {
      const parser = await getParser();
      const parserRes: BatchParseResult = await parser.batchParseStack(stacks);
    
      if (parserRes.length === 0) {
        return {
          isError: true,
          content: [{ type: "text", text: "No data could be parsed from the provided stack traces." }],
        }
      }
    
      return {
        content: [{
          type: "text", text: JSON.stringify(parserRes.map((e) => {
            if (e.success) {
              return e;
            } else {
              // Sanitize error messages to avoid exposing internal details
              const sanitizedMessage = e.error.message.replace(/[^\w\s.:\-]/g, '');
              return {
                success: false,
                msg: sanitizedMessage,
              }
            }
          }))
        }],
      }
    }
  • Zod input schema defining the 'stacks' array of objects with line, column, and sourceMapUrl for the parse_stack tool.
    schema: {
      stacks: z.array(
        z.object({
          line: z.number({
            description: "The line number in the stack trace.",
          }),
          column: z.number({
            description: "The column number in the stack trace.",
          }),
          sourceMapUrl: z.string({
            description: "The URL of the source map file corresponding to the stack trace.",
          }),
        })
      )
    },
  • src/tools.ts:348-354 (registration)
    Registration loop that registers the parse_stack tool (and others) with the MCP server using server.tool if not filtered out.
    toolDefinitions.forEach(tool => {
      if (shouldRegisterTool(tool.name, options.toolFilter)) {
        server.tool(tool.name, tool.description, tool.schema, async (params) => {
          return tool.handler(params, getParser);
        });
      }
    });
  • Low-level helper that invokes the WASM source_map_parser to generate source token with context lines for a given stack position.
    public async getSourceToken(line: number, column: number, sourceMap: string): Promise<Token> {
      await this.init();
    
      try {
        const res = sourceMapParser.generate_token_by_single_stack(line, column, sourceMap, this.contextOffsetLine);
    
        let rawToken: unknown;
        if (typeof res === 'string') {
          rawToken = JSON.parse(res);
        } else if (res && typeof res === 'object') {
          rawToken = res;
        } else {
          throw new Error(`unexpected token response type: ${typeof res}`);
        }
    
        // Transform the raw token to match the expected interface
        if (!this.isRawToken(rawToken)) {
          throw new Error('Invalid raw token structure from WebAssembly');
        }
    
        const token = this.transformToken(rawToken);
    
        // Validate the token structure
        this.validateToken(token);
    
        return token;
    
      } catch (error) {
        throw new Error("parse token error: " + (error instanceof Error ? error.message : error), {
          cause: error,
        });
      }
    }
Behavior4/5

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

With no annotations provided, the description carries full burden and does well. It discloses key behavioral traits: the tool maps stack trace info to source code locations, fetches additional context lines, handles multiple stacks via an array, provides default values (ctxOffset defaults to 5), and returns parsed info or error messages on failure. This covers most expected behavioral aspects for this type of tool.

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?

The description is well-structured with clear sections (purpose, parameters, returns) and uses bullet points efficiently. Every sentence adds value: the first states the purpose, the second explains what it requires, the third describes the mapping functionality, and the fourth adds context fetching. No wasted words.

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

Completeness4/5

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

Given no annotations and no output schema, the description provides strong context: clear purpose, parameter details, behavioral traits, and return value information. It doesn't explicitly describe error handling beyond 'if parsing fails', but covers most aspects needed for a parsing tool with complex input structure.

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?

Schema description coverage is 0%, so the description must fully compensate. It provides comprehensive parameter semantics: 'stacks' is an array of objects with line, column, and sourceMapUrl properties, and 'ctxOffset' is optional with default value and purpose explained. The description adds substantial meaning beyond the bare schema structure.

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 the tool's purpose: 'parse error stack traces' with specific resources (source map URL, line/column numbers) and functionality (mapping to source code location, fetching context lines). It distinguishes itself from siblings like 'lookup_context' and 'unpack_sources' by focusing on stack trace parsing with source maps.

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

Usage Guidelines4/5

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

The description provides clear context for when to use this tool: when you have error stack traces with line/column numbers and source map URLs. It doesn't explicitly state when NOT to use it or name specific alternatives among siblings, but the context is sufficiently clear for informed selection.

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/MasonChow/source-map-parser-mcp'

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