fix(mcp): always register lsp server
This commit is contained in:
+64
-25
@@ -1,11 +1,24 @@
|
||||
import { existsSync } from "node:fs"
|
||||
import { resolve } from "node:path"
|
||||
import { dirname, resolve } from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
|
||||
const SUBMODULE_REL = "packages/lsp-tools-mcp"
|
||||
const CLI_REL = "dist/cli.js"
|
||||
const DIST_CLI_REL = "dist/cli.js"
|
||||
const SOURCE_CLI_REL = "src/cli.ts"
|
||||
const PROJECT_LSP_CONFIG = ".opencode/lsp.json"
|
||||
|
||||
type LspMcpConfigOptions = {
|
||||
readonly cwd?: string
|
||||
readonly moduleUrl?: string
|
||||
readonly exists?: (path: string) => boolean
|
||||
}
|
||||
|
||||
type LspCommandCandidate = {
|
||||
readonly command: string[]
|
||||
readonly path: string
|
||||
readonly exists: boolean
|
||||
}
|
||||
|
||||
export type LocalMcpConfig = {
|
||||
type: "local"
|
||||
command: string[]
|
||||
@@ -13,11 +26,26 @@ export type LocalMcpConfig = {
|
||||
environment?: Record<string, string>
|
||||
}
|
||||
|
||||
function addCliPathCandidates(startDirectory: string, maxParentDepth: number, target: Set<string>): void {
|
||||
let currentDirectory = startDirectory
|
||||
function addAncestorCommandCandidates(
|
||||
startDirectory: string,
|
||||
target: LspCommandCandidate[],
|
||||
seenPaths: Set<string>,
|
||||
pathExists: (path: string) => boolean,
|
||||
): void {
|
||||
let currentDirectory = resolve(startDirectory)
|
||||
|
||||
for (let depth = 0; depth <= maxParentDepth; depth += 1) {
|
||||
target.add(resolve(currentDirectory, SUBMODULE_REL, CLI_REL))
|
||||
while (true) {
|
||||
const distCliPath = resolve(currentDirectory, SUBMODULE_REL, DIST_CLI_REL)
|
||||
if (!seenPaths.has(distCliPath)) {
|
||||
seenPaths.add(distCliPath)
|
||||
target.push({ command: ["node", distCliPath, "mcp"], path: distCliPath, exists: pathExists(distCliPath) })
|
||||
}
|
||||
|
||||
const sourceCliPath = resolve(currentDirectory, SUBMODULE_REL, SOURCE_CLI_REL)
|
||||
if (!seenPaths.has(sourceCliPath)) {
|
||||
seenPaths.add(sourceCliPath)
|
||||
target.push({ command: ["bun", sourceCliPath, "mcp"], path: sourceCliPath, exists: pathExists(sourceCliPath) })
|
||||
}
|
||||
|
||||
const parentDirectory = resolve(currentDirectory, "..")
|
||||
if (parentDirectory === currentDirectory) {
|
||||
@@ -28,32 +56,43 @@ function addCliPathCandidates(startDirectory: string, maxParentDepth: number, ta
|
||||
}
|
||||
}
|
||||
|
||||
function resolveLspCliPathCandidates(): string[] {
|
||||
const candidates = new Set<string>()
|
||||
|
||||
function getModuleDirectory(moduleUrl: string): string | null {
|
||||
try {
|
||||
const currentFilePath = fileURLToPath(import.meta.url)
|
||||
const currentDirectory = resolve(currentFilePath, "..")
|
||||
addCliPathCandidates(currentDirectory, 6, candidates)
|
||||
return dirname(fileURLToPath(moduleUrl))
|
||||
} catch {
|
||||
// ignore and fall through to cwd-based candidates
|
||||
}
|
||||
|
||||
addCliPathCandidates(process.cwd(), 4, candidates)
|
||||
|
||||
return [...candidates]
|
||||
}
|
||||
|
||||
export function createLspMcpConfig(): LocalMcpConfig | null {
|
||||
const cliPath = resolveLspCliPathCandidates().find((candidatePath) => existsSync(candidatePath))
|
||||
|
||||
if (!cliPath) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function resolveLspCommand(options: LspMcpConfigOptions = {}): string[] {
|
||||
const pathExists = options.exists ?? existsSync
|
||||
const candidates: LspCommandCandidate[] = []
|
||||
const seenPaths = new Set<string>()
|
||||
const moduleDirectory = getModuleDirectory(options.moduleUrl ?? import.meta.url)
|
||||
|
||||
if (moduleDirectory) {
|
||||
addAncestorCommandCandidates(moduleDirectory, candidates, seenPaths, pathExists)
|
||||
}
|
||||
|
||||
addAncestorCommandCandidates(options.cwd ?? process.cwd(), candidates, seenPaths, pathExists)
|
||||
|
||||
const distCandidate = candidates.find((candidate) => candidate.path.endsWith(DIST_CLI_REL) && candidate.exists)
|
||||
if (distCandidate) {
|
||||
return distCandidate.command
|
||||
}
|
||||
|
||||
const sourceCandidate = candidates.find((candidate) => candidate.path.endsWith(SOURCE_CLI_REL) && candidate.exists)
|
||||
if (sourceCandidate) {
|
||||
return sourceCandidate.command
|
||||
}
|
||||
|
||||
return candidates[0]?.command ?? ["node", resolve(process.cwd(), SUBMODULE_REL, DIST_CLI_REL), "mcp"]
|
||||
}
|
||||
|
||||
export function createLspMcpConfig(options: LspMcpConfigOptions = {}): LocalMcpConfig {
|
||||
return {
|
||||
type: "local",
|
||||
command: ["node", cliPath, "mcp"],
|
||||
command: resolveLspCommand(options),
|
||||
enabled: true,
|
||||
environment: {
|
||||
LSP_TOOLS_MCP_PROJECT_CONFIG: PROJECT_LSP_CONFIG,
|
||||
|
||||
Reference in New Issue
Block a user