Skip to main content
POST
/
tools
/
get_code_actions
Get Code Actions
curl --request POST \
  --url https://api.example.com/tools/get_code_actions
{
  "actions": [
    {
      "title": "<string>",
      "kind": "<string>",
      "edit": {
        "changes": {}
      },
      "isPreferred": true
    }
  ]
}

Overview

Get actionable suggestions to fix errors, apply quick fixes, or refactor code. This provides the same “lightbulb” suggestions you see in your IDE.

Request

uri
string
required
File URI (e.g., file:///workspace/main.py)
range
object
required
Text range to get actions for
context
object
Context for determining actions

Response

actions
array
Array of available code actions

Example Request

curl -X POST https://api.pylancemcp.dev/v1/tools/call \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "get_code_actions",
    "arguments": {
      "uri": "file:///workspace/app/views.py",
      "range": {
        "start": {"line": 15, "character": 0},
        "end": {"line": 15, "character": 30}
      }
    }
  }'

Example Response

{
  "actions": [
    {
      "title": "Import 'List' from typing",
      "kind": "quickfix",
      "edit": {
        "changes": {
          "file:///workspace/app/views.py": [
            {
              "range": {
                "start": { "line": 0, "character": 0 },
                "end": { "line": 0, "character": 0 }
              },
              "newText": "from typing import List\n"
            }
          ]
        }
      },
      "isPreferred": true
    },
    {
      "title": "Add type annotation",
      "kind": "refactor",
      "edit": {
        "changes": {
          "file:///workspace/app/views.py": [
            {
              "range": {
                "start": { "line": 15, "character": 15 },
                "end": { "line": 15, "character": 15 }
              },
              "newText": ": List[Item]"
            }
          ]
        }
      },
      "isPreferred": false
    }
  ]
}

Action Types

Quick Fixes

Fix errors and warnings automatically:
ActionFixes
Add import"List" is not defined → Add from typing import List
Remove unusedImport "os" is not accessed → Remove import os
Add type hintMissing type annotation → Add : int
Fix indentationIndentation error → Correct spacing

Refactorings

Transform code structure:
ActionTransform
Extract methodSelection → New function
Extract variableExpression → Named variable
Inline variableVariable → Direct value
Convert to f-string"Hello " + namef"Hello {name}"

Source Actions

Organize code:
ActionEffect
Organize importsSort and group imports
Remove unused importsDelete unreferenced imports
Add all missing importsImport all undefined names
Sort importsAlphabetically sort imports

Use Cases

Auto-Fix Errors

# Code with error:
items = []  # List not imported

# Get code actions at error location
actions = client.get_code_actions(uri=file_uri, range=error_range)

# Find and apply the "Add import" quickfix
for action in actions.actions:
    if action.kind == "quickfix" and "import" in action.title.lower():
        apply_workspace_edit(action.edit)
        print(f"✅ Applied: {action.title}")
        break

Extract Function

# Select code to extract:
# Lines 15-20 contain repeated calculation logic

actions = client.get_code_actions(
    uri=file_uri,
    range={"start": {"line": 15, "character": 0}, 
           "end": {"line": 20, "character": 50}}
)

# Find "Extract method" action
for action in actions.actions:
    if "extract" in action.kind.lower():
        # Preview the change
        print(f"Will create new function: {action.title}")
        # Apply if desired
        apply_workspace_edit(action.edit)

Organize Imports

# Organize all imports in a file
actions = client.get_code_actions(
    uri=file_uri,
    range={"start": {"line": 0, "character": 0},
           "end": {"line": 0, "character": 0}},
    context={"only": ["source.organizeImports"]}
)

if actions.actions:
    organize_action = actions.actions[0]
    apply_workspace_edit(organize_action.edit)
    print("✨ Imports organized")

Convert String Formatting

# Convert old-style formatting to f-strings
# Before: message = "Hello " + name + "!"

actions = client.get_code_actions(uri=file_uri, range=selection_range)

for action in actions.actions:
    if "f-string" in action.title.lower():
        apply_workspace_edit(action.edit)
        # After: message = f"Hello {name}!"
        break

Filtering Actions

Request only specific action types:
actions = client.get_code_actions(
    uri=file_uri,
    range=range,
    context={"only": ["quickfix"]}
)

Action Kinds

Full list of action kinds:
KindCategoryExamples
quickfixError fixesAdd import, fix syntax
refactorCode transformationExtract, inline, rename
refactor.extractExtract operationsExtract method/variable
refactor.inlineInline operationsInline variable/function
refactor.rewriteRewrite patternsConvert to f-string
sourceSource-levelOrganize/sort code
source.organizeImportsImport managementSort and group imports

Preferred Actions

When multiple actions are available, isPreferred: true indicates the recommended one:
# Get actions
actions = client.get_code_actions(...)

# Apply preferred action automatically
preferred = next((a for a in actions.actions if a.is_preferred), None)
if preferred:
    apply_workspace_edit(preferred.edit)

Performance Tips

Request on Demand: Only fetch actions when user explicitly asks (lightbulb icon clicked)
Use Filters: Request specific action kinds to reduce response size
Cache by Range: Actions for same range don’t change unless code changes

Error Responses

CodeReasonSolution
FILE_NOT_FOUNDFile doesn’t existVerify file path
INVALID_RANGERange out of boundsCheck start/end positions
NO_ACTIONS_AVAILABLENo actions for this rangeTry different selection
PARSE_ERRORCannot parse fileFix syntax errors first

Rate Limits

TierRequests/HourRequests/Day
Free20100
Hobby5005,000
Pro5,00050,000
EnterpriseUnlimitedUnlimited