| name | lsp-extract-function |
| description | Extract a selected code block into a named function. Primary path uses the language server's extract-function code action; falls back to manual extraction when no code action is available. Validates captured variables, scope shadowing, and compilation after extraction. |
| argument-hint | [file-path] [start-line] [end-line] [new-function-name] |
| user-invocable | true |
| allowed-tools | mcp__lsp__list_symbols mcp__lsp__suggest_fixes mcp__lsp__execute_command mcp__lsp__apply_edit mcp__lsp__get_diagnostics mcp__lsp__open_document mcp__lsp__format_document mcp__lsp__get_server_capabilities |
| license | MIT |
| compatibility | Requires the agent-lsp MCP server (github.com/blackwell-systems/agent-lsp) |
| metadata | {"required-capabilities":"codeActionProvider","optional-capabilities":"documentFormattingProvider documentSymbolProvider"} |
Requires the agent-lsp MCP server.
lsp-extract-function: Extract Code Block into a Named Function
This skill RESTRUCTURES existing code — it takes code that already exists
and moves it into a new function. This is distinct from /lsp-generate, which
creates NEW code that does not yet exist (stubs, mocks, interface implementations).
Use this skill when the code is already written; use /lsp-generate when you
need to generate code from scratch.
Invocation: User provides file_path (absolute path), start_line and
end_line (1-indexed range), and new_function_name (desired name for the
extracted function).
Prerequisites
If LSP is not yet initialized, call mcp__lsp__start_lsp with the workspace
root first. Auto-inference applies when file paths are provided, but an explicit
start is required when switching workspaces.
Step 1 — Get context (document symbols)
Call mcp__lsp__open_document to open the file, then call
mcp__lsp__list_symbols to understand the containing function and scope:
mcp__lsp__open_document({ "file_path": "<file_path>" })
mcp__lsp__list_symbols({ "file_path": "<file_path>" })
This establishes:
- Which function contains the selection
- Whether
new_function_name already exists in the file (name collision check)
Mandatory name collision check: If new_function_name already exists as a
symbol in the document symbols list, report the conflict and stop immediately:
Cannot extract: function new_function_name already exists in this file.
Choose a different name and retry.
Step 2 — Check server capabilities
Call mcp__lsp__get_server_capabilities to understand what the language server
supports:
mcp__lsp__get_server_capabilities({})
Check for codeActionProvider in the response. Note whether execute_command
is listed in executeCommandProvider.commands. This determines whether the
primary path (Step 3) is available.
Step 3 — Primary path: LSP code action
Call mcp__lsp__suggest_fixes with the selection range: