2026-05-18 12:11:21 +09:00
|
|
|
import { existsSync } from "node:fs"
|
2026-05-18 16:45:02 +09:00
|
|
|
import { dirname, resolve } from "node:path"
|
2026-05-18 12:11:21 +09:00
|
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
|
|
2026-05-18 16:00:40 +09:00
|
|
|
const SUBMODULE_REL = "packages/lsp-tools-mcp"
|
2026-05-18 16:45:02 +09:00
|
|
|
const DIST_CLI_REL = "dist/cli.js"
|
|
|
|
|
const SOURCE_CLI_REL = "src/cli.ts"
|
2026-05-18 12:11:21 +09:00
|
|
|
const PROJECT_LSP_CONFIG = ".opencode/lsp.json"
|
|
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
type LspMcpConfigOptions = {
|
|
|
|
|
readonly cwd?: string
|
|
|
|
|
readonly moduleUrl?: string
|
|
|
|
|
readonly exists?: (path: string) => boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LspCommandCandidate = {
|
|
|
|
|
readonly command: string[]
|
|
|
|
|
readonly path: string
|
|
|
|
|
readonly exists: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 12:11:21 +09:00
|
|
|
export type LocalMcpConfig = {
|
|
|
|
|
type: "local"
|
|
|
|
|
command: string[]
|
|
|
|
|
enabled: boolean
|
|
|
|
|
environment?: Record<string, string>
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
function addAncestorCommandCandidates(
|
|
|
|
|
startDirectory: string,
|
|
|
|
|
target: LspCommandCandidate[],
|
|
|
|
|
seenPaths: Set<string>,
|
|
|
|
|
pathExists: (path: string) => boolean,
|
|
|
|
|
): void {
|
|
|
|
|
let currentDirectory = resolve(startDirectory)
|
2026-05-18 13:20:41 +09:00
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
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) })
|
|
|
|
|
}
|
2026-05-18 13:20:41 +09:00
|
|
|
|
|
|
|
|
const parentDirectory = resolve(currentDirectory, "..")
|
|
|
|
|
if (parentDirectory === currentDirectory) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentDirectory = parentDirectory
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
function getModuleDirectory(moduleUrl: string): string | null {
|
2026-05-18 12:11:21 +09:00
|
|
|
try {
|
2026-05-18 16:45:02 +09:00
|
|
|
return dirname(fileURLToPath(moduleUrl))
|
2026-05-18 12:11:21 +09:00
|
|
|
} catch {
|
2026-05-18 16:45:02 +09:00
|
|
|
return null
|
2026-05-18 12:11:21 +09:00
|
|
|
}
|
2026-05-18 16:45:02 +09:00
|
|
|
}
|
2026-05-18 12:11:21 +09:00
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
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)
|
2026-05-18 12:11:21 +09:00
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
if (moduleDirectory) {
|
|
|
|
|
addAncestorCommandCandidates(moduleDirectory, candidates, seenPaths, pathExists)
|
|
|
|
|
}
|
2026-05-18 12:11:21 +09:00
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
addAncestorCommandCandidates(options.cwd ?? process.cwd(), candidates, seenPaths, pathExists)
|
2026-05-18 12:11:21 +09:00
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
const distCandidate = candidates.find((candidate) => candidate.path.endsWith(DIST_CLI_REL) && candidate.exists)
|
|
|
|
|
if (distCandidate) {
|
|
|
|
|
return distCandidate.command
|
2026-05-18 12:11:21 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-18 16:45:02 +09:00
|
|
|
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 {
|
2026-05-18 12:11:21 +09:00
|
|
|
return {
|
|
|
|
|
type: "local",
|
2026-05-18 16:45:02 +09:00
|
|
|
command: resolveLspCommand(options),
|
2026-05-18 12:11:21 +09:00
|
|
|
enabled: true,
|
|
|
|
|
environment: {
|
|
|
|
|
LSP_TOOLS_MCP_PROJECT_CONFIG: PROJECT_LSP_CONFIG,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|