Files
oh-my-opencode/src/mcp/lsp.ts
T

102 lines
3.0 KiB
TypeScript
Raw Normal View History

import { existsSync } from "node:fs"
2026-05-18 16:45:02 +09:00
import { dirname, resolve } from "node:path"
import { fileURLToPath } from "node:url"
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"
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
}
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 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) })
}
const parentDirectory = resolve(currentDirectory, "..")
if (parentDirectory === currentDirectory) {
return
}
currentDirectory = parentDirectory
}
}
2026-05-18 16:45:02 +09:00
function getModuleDirectory(moduleUrl: string): string | null {
try {
2026-05-18 16:45:02 +09:00
return dirname(fileURLToPath(moduleUrl))
} catch {
2026-05-18 16:45:02 +09:00
return null
}
2026-05-18 16:45:02 +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 16:45:02 +09:00
if (moduleDirectory) {
addAncestorCommandCandidates(moduleDirectory, candidates, seenPaths, pathExists)
}
2026-05-18 16:45:02 +09:00
addAncestorCommandCandidates(options.cwd ?? process.cwd(), candidates, seenPaths, pathExists)
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 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 {
return {
type: "local",
2026-05-18 16:45:02 +09:00
command: resolveLspCommand(options),
enabled: true,
environment: {
LSP_TOOLS_MCP_PROJECT_CONFIG: PROJECT_LSP_CONFIG,
},
}
}