Skip to content

[Bug]: goimports may resolve mcp package incorrectly when LLM omits import #142

@spachava753

Description

@spachava753

Affected Area

Code mode execution

Model Provider

N/A

Model ID

N/A (affects all models)

Bug Description

With the introduction of multimedia content return support in commit 75fb1d8 (PR #141, closes #139), the Run function signature changed from error to ([]mcp.Content, error). This requires the github.com/modelcontextprotocol/go-sdk/mcp package to be imported.

The tool description in tooldesc.go instructs the LLM to include this import, but models sometimes forget. When this happens, autoCorrectImports() in executor.go runs goimports which may resolve mcp to a different package (e.g., some other package in the ecosystem named "mcp"), causing compilation errors or unexpected behavior.

Expected Behavior

When the LLM generates code that uses mcp.Content, mcp.TextContent, mcp.ImageContent, or mcp.AudioContent without importing the correct package, CPE should automatically ensure github.com/modelcontextprotocol/go-sdk/mcp is in the imports.

Steps to Reproduce

  1. Have a model generate code that returns multimedia content but forgets the mcp import:
package main

import (
    "context"
    "os"
)

func Run(ctx context.Context) ([]mcp.Content, error) {
    data, _ := os.ReadFile("image.png")
    return []mcp.Content{
        &mcp.ImageContent{Data: data, MIMEType: "image/png"},
    }, nil
}
  1. goimports runs and may resolve mcp to the wrong package
  2. Compilation fails or unexpected behavior occurs

Relevant Configuration

N/A - affects code mode execution infrastructure

Error Output / Logs

Varies depending on which wrong package goimports resolves to.

Environment

  • CPE version: 75fb1d8
  • Go version: 1.24.0+
  • OS: All

Additional Context

Relevant files:

  • internal/codemode/executor.go - autoCorrectImports() function (line 225-253)
  • internal/codemode/tooldesc.go - Tool description that tells LLM to import mcp

Proposed fix:
Add a pre-processing step before calling goimports that:

  1. Parses the generated code to check if it references mcp. identifiers
  2. If mcp is referenced but github.com/modelcontextprotocol/go-sdk/mcp is not in imports, add it

This ensures goimports won't resolve mcp to the wrong package since it's already present.

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions