Skip to content

AI-native design editor. Open-source Figma alternative.

refactor(mcp): keep disabled tools typed across boundaries

Danila Poyarkov9c4d188ea4d5
diff --git a/packages/mcp/package.json b/packages/mcp/package.json
index 9a08d8118..055545b7c 100644
--- a/packages/mcp/package.json
+++ b/packages/mcp/package.json
@@ -36,7 +36,7 @@
},
"./tools": {
"types": "./dist/tools.d.mts",
- "bun": "./src/tool/metadata.ts",
+ "bun": "./src/tool/index.ts",
"import": "./dist/tools.mjs",
"default": "./dist/tools.mjs"
}
diff --git a/packages/mcp/src/tool/index.ts b/packages/mcp/src/tool/index.ts
new file mode 100644
index 000000000..cc122df59
--- /dev/null
+++ b/packages/mcp/src/tool/index.ts
@@ -0,0 +1,2 @@
+export * from './metadata'
+export { serializeDisabledTools } from './policy'
diff --git a/packages/mcp/src/tool/policy.ts b/packages/mcp/src/tool/policy.ts
index 0c198a0e6..dbaac1d30 100644
--- a/packages/mcp/src/tool/policy.ts
+++ b/packages/mcp/src/tool/policy.ts
@@ -27,6 +27,10 @@ export function applyToolPolicy(
}))
}
+export function serializeDisabledTools(names: readonly string[]): string {
+ return names.join(',')
+}
+
export function readToolPolicyFromEnv(env: NodeJS.ProcessEnv = process.env): ToolPolicy {
return {
allowEval: env.OPENPENCIL_MCP_EVAL === '1',
diff --git a/packages/mcp/tsdown.config.ts b/packages/mcp/tsdown.config.ts
index 3e29532fb..2fe961eb9 100644
--- a/packages/mcp/tsdown.config.ts
+++ b/packages/mcp/tsdown.config.ts
@@ -7,7 +7,7 @@ export default defineConfig({
stdio: './src/stdio.ts',
transport: './src/transport/paths.ts',
discovery: './src/transport/discovery.ts',
- tools: './src/tool/metadata.ts'
+ tools: './src/tool/index.ts'
},
platform: 'node',
format: ['esm'],
diff --git a/src/app/automation/bridge/vite-plugin.ts b/src/app/automation/bridge/vite-plugin.ts
index 5dc35743d..5a00bb7a4 100644
--- a/src/app/automation/bridge/vite-plugin.ts
+++ b/src/app/automation/bridge/vite-plugin.ts
@@ -4,11 +4,12 @@ import type { IncomingMessage } from 'node:http'
import type { Plugin } from 'vite'
import { AUTOMATION_HTTP_PORT } from '@open-pencil/core/constants'
+import { serializeDisabledTools } from '@open-pencil/mcp/tools'
import { getSocketPath, platformHasUnixSockets } from '@open-pencil/mcp/transport'
import {
DEV_MCP_RESTART_PATH,
- isDevMCPConfiguration,
+ parseDevMCPConfiguration,
type DevMCPConfiguration
} from '../mcp/dev-control'
@@ -35,7 +36,7 @@ export function createAutomationEnvironment(
OPENPENCIL_MCP_AUTH_TOKEN: configuration.authenticationEnabled ? (authToken ?? '') : '',
OPENPENCIL_MCP_CORS_ORIGIN: corsOrigin,
OPENPENCIL_MCP_ROOT: configuration.rootDirectory.trim() || process.cwd(),
- OPENPENCIL_MCP_DISABLED_TOOLS: configuration.disabledTools
+ OPENPENCIL_MCP_DISABLED_TOOLS: serializeDisabledTools(configuration.disabledTools)
}
}
@@ -96,7 +97,7 @@ export function automationPlugin(authToken: string | null, corsOrigin: string):
let configuration: DevMCPConfiguration = {
authenticationEnabled: true,
rootDirectory: '',
- disabledTools: ''
+ disabledTools: []
}
function enqueue(operation: () => Promise<void>): Promise<void> {
@@ -184,8 +185,9 @@ export function automationPlugin(authToken: string | null, corsOrigin: string):
}
void (async () => {
try {
- const nextConfiguration = await readDevMCPConfiguration(request)
- if (!isDevMCPConfiguration(nextConfiguration)) {
+ const rawConfiguration = await readDevMCPConfiguration(request)
+ const nextConfiguration = parseDevMCPConfiguration(rawConfiguration)
+ if (!nextConfiguration) {
response.statusCode = 400
response.end('Invalid MCP configuration')
return
diff --git a/src/app/automation/mcp/dev-control.ts b/src/app/automation/mcp/dev-control.ts
index cea40e1ef..4d5997ace 100644
--- a/src/app/automation/mcp/dev-control.ts
+++ b/src/app/automation/mcp/dev-control.ts
@@ -1,21 +1,47 @@
export const DEV_MCP_RESTART_PATH = '/__openpencil/mcp/restart'
+const MAX_ROOT_DIRECTORY_LENGTH = 4_096
+const MAX_DISABLED_TOOL_COUNT = 512
+const MAX_DISABLED_TOOL_NAME_LENGTH = 128
+const MAX_DISABLED_TOOLS_LENGTH = 65_536
+const TOOL_NAME_PATTERN = /^[a-z0-9_:-]+$/
+
export interface DevMCPConfiguration {
authenticationEnabled: boolean
rootDirectory: string
- disabledTools: string
+ disabledTools: string[]
+}
+
+function parseDisabledTools(value: unknown): string[] | null {
+ if (!Array.isArray(value) || value.length > MAX_DISABLED_TOOL_COUNT) return null
+ const disabledTools = new Set<string>()
+ let encodedLength = 0
+ for (const candidate of value) {
+ if (typeof candidate !== 'string') return null
+ const name = candidate.trim()
+ if (!name || name.length > MAX_DISABLED_TOOL_NAME_LENGTH || !TOOL_NAME_PATTERN.test(name)) {
+ return null
+ }
+ if (disabledTools.has(name)) continue
+ encodedLength += name.length + (disabledTools.size > 0 ? 1 : 0)
+ if (encodedLength > MAX_DISABLED_TOOLS_LENGTH) return null
+ disabledTools.add(name)
+ }
+ return [...disabledTools]
}
-export function isDevMCPConfiguration(value: unknown): value is DevMCPConfiguration {
- if (!value || typeof value !== 'object' || Array.isArray(value)) return false
+export function parseDevMCPConfiguration(value: unknown): DevMCPConfiguration | null {
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return null
const authenticationEnabled = Reflect.get(value, 'authenticationEnabled')
const rootDirectory = Reflect.get(value, 'rootDirectory')
- const disabledTools = Reflect.get(value, 'disabledTools')
- return (
- typeof authenticationEnabled === 'boolean' &&
- typeof rootDirectory === 'string' &&
- rootDirectory.length <= 4_096 &&
- typeof disabledTools === 'string' &&
- disabledTools.length <= 65_536
- )
+ const disabledTools = parseDisabledTools(Reflect.get(value, 'disabledTools'))
+ if (
+ typeof authenticationEnabled !== 'boolean' ||
+ typeof rootDirectory !== 'string' ||
+ rootDirectory.length > MAX_ROOT_DIRECTORY_LENGTH ||
+ !disabledTools
+ ) {
+ return null
+ }
+ return { authenticationEnabled, rootDirectory, disabledTools }
}
diff --git a/src/app/automation/mcp/preferences.ts b/src/app/automation/mcp/preferences.ts
index 014663353..bf5a0a889 100644
--- a/src/app/automation/mcp/preferences.ts
+++ b/src/app/automation/mcp/preferences.ts
@@ -33,7 +33,3 @@ export function setMCPToolCategoryEnabled(effect: ToolEffect, enabled: boolean):
}
disabledMCPTools.value = [...disabled]
}
-
-export function disabledMCPToolsCSV(): string {
- return disabledMCPTools.value.join(',')
-}
diff --git a/src/app/automation/mcp/spawn.ts b/src/app/automation/mcp/spawn.ts
index 859e61784..3123949ec 100644
--- a/src/app/automation/mcp/spawn.ts
+++ b/src/app/automation/mcp/spawn.ts
@@ -3,15 +3,18 @@ import { promiseTimeout } from '@vueuse/core'
import { AUTOMATION_HTTP_PORT } from '@open-pencil/core/constants'
import { randomHex } from '@open-pencil/core/random'
import type { DiscoveryInfo } from '@open-pencil/mcp/discovery'
-import type { ToolDescriptor } from '@open-pencil/mcp/tools'
-import { parseToolDescriptor } from '@open-pencil/mcp/tools'
+import {
+ parseToolDescriptor,
+ serializeDisabledTools,
+ type ToolDescriptor
+} from '@open-pencil/mcp/tools'
import { decodeTauriStderr } from '@/app/shell/ui'
import { resolvePlatformCommand } from '@/app/tauri/command'
import { isTauri } from '@/app/tauri/env'
import { DEV_MCP_RESTART_PATH, type DevMCPConfiguration } from './dev-control'
-import { disabledMCPToolsCSV, mcpAuthenticationEnabled, mcpRootDirectory } from './preferences'
+import { disabledMCPTools, mcpAuthenticationEnabled, mcpRootDirectory } from './preferences'
export interface AutomationHealth {
status: 'ok' | 'no_app'
@@ -336,7 +339,7 @@ async function configureDevMCP(): Promise<AutomationServerHandle> {
const configuration: DevMCPConfiguration = {
authenticationEnabled: mcpAuthenticationEnabled.value,
rootDirectory: mcpRootDirectory.value,
- disabledTools: disabledMCPToolsCSV()
+ disabledTools: [...disabledMCPTools.value]
}
const response = await fetch(DEV_MCP_RESTART_PATH, {
method: 'POST',
@@ -387,7 +390,7 @@ async function startMCPIfNeeded(): Promise<AutomationServerHandle | null> {
OPENPENCIL_MCP_TCP: '1',
OPENPENCIL_MCP_ROOT: mcpRoot,
OPENPENCIL_MCP_APP_TIMEOUT_MS: String(MCP_APP_ATTACH_TIMEOUT_MS),
- OPENPENCIL_MCP_DISABLED_TOOLS: disabledMCPToolsCSV()
+ OPENPENCIL_MCP_DISABLED_TOOLS: serializeDisabledTools(disabledMCPTools.value)
}
})
diff --git a/tests/engine/app/automation/mcp-vite-plugin.test.ts b/tests/engine/app/automation/mcp-vite-plugin.test.ts
index 485d2498c..03f2bd912 100644
--- a/tests/engine/app/automation/mcp-vite-plugin.test.ts
+++ b/tests/engine/app/automation/mcp-vite-plugin.test.ts
@@ -8,6 +8,7 @@ import {
devMCPConfigurationErrorStatus,
readDevMCPConfiguration
} from '@/app/automation/bridge/vite-plugin'
+import { parseDevMCPConfiguration } from '@/app/automation/mcp/dev-control'
describe('MCP Vite development server', () => {
test('passes an explicit empty auth token when authentication is disabled', () => {
@@ -17,7 +18,7 @@ describe('MCP Vite development server', () => {
configuration: {
authenticationEnabled: false,
rootDirectory: '/designs',
- disabledTools: 'eval,delete_node'
+ disabledTools: ['eval', 'delete_node']
},
corsOrigin: 'http://localhost:1420',
socketPath: '/tmp/open-pencil.sock'
@@ -28,12 +29,33 @@ describe('MCP Vite development server', () => {
expect(env.OPENPENCIL_MCP_DISABLED_TOOLS).toBe('eval,delete_node')
})
+ test('normalizes and validates typed disabled tool configuration', () => {
+ expect(
+ parseDevMCPConfiguration({
+ authenticationEnabled: true,
+ rootDirectory: '/designs',
+ disabledTools: [' eval ', 'delete_node', 'eval']
+ })
+ ).toEqual({
+ authenticationEnabled: true,
+ rootDirectory: '/designs',
+ disabledTools: ['eval', 'delete_node']
+ })
+ expect(
+ parseDevMCPConfiguration({
+ authenticationEnabled: true,
+ rootDirectory: '',
+ disabledTools: ['invalid tool']
+ })
+ ).toBeNull()
+ })
+
test('decodes UTF-8 configuration split across request chunks', async () => {
const body = Buffer.from(
JSON.stringify({
authenticationEnabled: true,
rootDirectory: '/设计',
- disabledTools: ''
+ disabledTools: []
})
)
const split = body.indexOf(Buffer.from('设')) + 1
@@ -42,7 +64,7 @@ describe('MCP Vite development server', () => {
await expect(readDevMCPConfiguration(request as never)).resolves.toEqual({
authenticationEnabled: true,
rootDirectory: '/设计',
- disabledTools: ''
+ disabledTools: []
})
})