Fix local MCP scope path containment
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,17 +1,6 @@
|
|||||||
import { existsSync, realpathSync } from "fs"
|
import { containsPath } from "../../shared/contains-path"
|
||||||
import { resolve } from "path"
|
|
||||||
import type { ClaudeCodeMcpServer } from "./types"
|
import type { ClaudeCodeMcpServer } from "./types"
|
||||||
|
|
||||||
function normalizePath(path: string): string {
|
|
||||||
const resolvedPath = resolve(path)
|
|
||||||
|
|
||||||
if (!existsSync(resolvedPath)) {
|
|
||||||
return resolvedPath
|
|
||||||
}
|
|
||||||
|
|
||||||
return realpathSync(resolvedPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function shouldLoadMcpServer(
|
export function shouldLoadMcpServer(
|
||||||
server: Pick<ClaudeCodeMcpServer, "scope" | "projectPath">,
|
server: Pick<ClaudeCodeMcpServer, "scope" | "projectPath">,
|
||||||
cwd = process.cwd()
|
cwd = process.cwd()
|
||||||
@@ -24,5 +13,5 @@ export function shouldLoadMcpServer(
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return normalizePath(server.projectPath) === normalizePath(cwd)
|
return containsPath(server.projectPath, cwd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,22 @@
|
|||||||
import { existsSync, realpathSync } from "fs"
|
import { existsSync, realpathSync } from "fs"
|
||||||
import { basename, dirname, isAbsolute, join, normalize, relative, resolve } from "path"
|
import { basename, dirname, isAbsolute, join, normalize, relative, resolve } from "path"
|
||||||
|
|
||||||
|
function findNearestExistingAncestor(resolvedPath: string): string {
|
||||||
|
let candidatePath = resolvedPath
|
||||||
|
|
||||||
|
while (!existsSync(candidatePath)) {
|
||||||
|
const parentPath = dirname(candidatePath)
|
||||||
|
|
||||||
|
if (parentPath === candidatePath) {
|
||||||
|
return candidatePath
|
||||||
|
}
|
||||||
|
|
||||||
|
candidatePath = parentPath
|
||||||
|
}
|
||||||
|
|
||||||
|
return candidatePath
|
||||||
|
}
|
||||||
|
|
||||||
function toCanonicalPath(pathToNormalize: string): string {
|
function toCanonicalPath(pathToNormalize: string): string {
|
||||||
const resolvedPath = resolve(pathToNormalize)
|
const resolvedPath = resolve(pathToNormalize)
|
||||||
|
|
||||||
@@ -12,12 +28,13 @@ function toCanonicalPath(pathToNormalize: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentDirectory = dirname(resolvedPath)
|
const nearestExistingAncestor = findNearestExistingAncestor(resolvedPath)
|
||||||
const canonicalParentDirectory = existsSync(parentDirectory)
|
const canonicalAncestor = existsSync(nearestExistingAncestor)
|
||||||
? realpathSync.native(parentDirectory)
|
? realpathSync.native(nearestExistingAncestor)
|
||||||
: parentDirectory
|
: nearestExistingAncestor
|
||||||
|
const relativePathFromAncestor = relative(nearestExistingAncestor, resolvedPath)
|
||||||
|
|
||||||
return normalize(join(canonicalParentDirectory, basename(resolvedPath)))
|
return normalize(join(canonicalAncestor, relativePathFromAncestor || basename(resolvedPath)))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function containsPath(rootPath: string, candidatePath: string): boolean {
|
export function containsPath(rootPath: string, candidatePath: string): boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user