Skip to content

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

fix(app): harden MCP configuration controls

Danila Poyarkovb9bdc043f757
diff --git a/src/app/automation/bridge/vite-plugin.ts b/src/app/automation/bridge/vite-plugin.ts
index e95c1e861..fa6989b46 100644
--- a/src/app/automation/bridge/vite-plugin.ts
+++ b/src/app/automation/bridge/vite-plugin.ts
@@ -39,13 +39,19 @@ export function createAutomationEnvironment(
}
}
-async function readConfiguration(request: IncomingMessage): Promise<unknown> {
- let body = ''
+const MAX_CONFIGURATION_BYTES = 70_000
+const CHILD_EXIT_TIMEOUT_MS = 2_000
+
+export async function readDevMCPConfiguration(request: IncomingMessage): Promise<unknown> {
+ const chunks: Buffer[] = []
+ let byteLength = 0
for await (const chunk of request) {
- body += String(chunk)
- if (body.length > 70_000) throw new Error('Request body is too large')
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
+ byteLength += buffer.byteLength
+ if (byteLength > MAX_CONFIGURATION_BYTES) throw new Error('Request body is too large')
+ chunks.push(buffer)
}
- return JSON.parse(body)
+ return JSON.parse(Buffer.concat(chunks).toString('utf8'))
}
// TODO: production — bundle MCP server as Tauri sidecar or spawn via shell plugin
@@ -72,11 +78,16 @@ export function automationPlugin(authToken: string | null, corsOrigin: string):
running.once('exit', () => resolve())
})
running.kill()
- const timeout = new Promise<void>((resolve) => {
- setTimeout(resolve, 2_000)
+ let timeout: ReturnType<typeof setTimeout> | undefined
+ const timedOut = new Promise<boolean>((resolve) => {
+ timeout = setTimeout(() => resolve(true), CHILD_EXIT_TIMEOUT_MS)
})
- await Promise.race([exited, timeout])
- if (running.exitCode === null) running.kill('SIGKILL')
+ const exitedGracefully = await Promise.race([exited.then(() => false), timedOut])
+ if (timeout) clearTimeout(timeout)
+ if (!exitedGracefully && running.exitCode === null) {
+ running.kill('SIGKILL')
+ await exited
+ }
}
async function startChild(): Promise<void> {
@@ -138,7 +149,7 @@ export function automationPlugin(authToken: string | null, corsOrigin: string):
}
void (async () => {
try {
- const nextConfiguration = await readConfiguration(request)
+ const nextConfiguration = await readDevMCPConfiguration(request)
if (!isDevMCPConfiguration(nextConfiguration)) {
response.statusCode = 400
response.end('Invalid MCP configuration')
diff --git a/src/components/settings/mcp/MCPSettingsPanel.vue b/src/components/settings/mcp/MCPSettingsPanel.vue
index 336551681..209603edc 100644
--- a/src/components/settings/mcp/MCPSettingsPanel.vue
+++ b/src/components/settings/mcp/MCPSettingsPanel.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import type { ToolEffect } from '@open-pencil/mcp/tools'
import { computed, onMounted, ref } from 'vue'
import { useI18n } from '@open-pencil/vue'
@@ -11,22 +12,23 @@ import {
setMCPToolEnabled
} from '@/app/automation/mcp/preferences'
import { mcpRuntime, refreshMCPRuntime, restartMCPRuntime } from '@/app/automation/mcp/runtime'
+import { isTauri } from '@/app/tauri/env'
import AppInput from '@/components/ui/AppInput.vue'
import AppSwitch from '@/components/ui/AppSwitch.vue'
const { dialogs } = useI18n()
const toolSearch = ref('')
const disabledToolNames = computed(() => new Set(disabledMCPTools.value))
-function categoryStatus(documentAccess: 'inspect' | 'modify') {
- const tools = configurableMCPTools.value.filter((tool) => tool.documentAccess === documentAccess)
+function categoryStatus(effect: ToolEffect) {
+ const tools = configurableMCPTools.value.filter((tool) => tool.effect === effect)
const enabled = tools.filter((tool) => !disabledToolNames.value.has(tool.name)).length
return {
enabled: enabled > 0,
state: enabled > 0 && enabled < tools.length ? ('mixed' as const) : ('idle' as const)
}
}
-const inspectionToolsStatus = computed(() => categoryStatus('inspect'))
-const modificationToolsStatus = computed(() => categoryStatus('modify'))
+const inspectionToolsStatus = computed(() => categoryStatus('read'))
+const modificationToolsStatus = computed(() => categoryStatus('write'))
const enabledToolCount = computed(
() => configurableMCPTools.value.filter((tool) => !disabledToolNames.value.has(tool.name)).length
)
@@ -48,6 +50,7 @@ function restart(): void {
}
async function chooseRootDirectory(): Promise<void> {
+ if (!isTauri()) return
const { open } = await import('@tauri-apps/plugin-dialog')
const directory = await open({ directory: true, multiple: false })
if (typeof directory === 'string') mcpRootDirectory.value = directory
@@ -129,6 +132,7 @@ function enableAllTools(): void {
{{ dialogs.mcpUseDefaultRoot }}
</button>
<button
+ v-if="isTauri()"
type="button"
class="rounded border border-border px-2 py-1 text-[10px] text-surface hover:bg-hover"
data-test-id="settings-mcp-root-directory"
@@ -187,23 +191,23 @@ function enableAllTools(): void {
<div class="grid grid-cols-2 gap-2 border-b border-border p-2.5">
<div class="flex items-center justify-between gap-2 rounded bg-input px-2.5 py-2">
- <span class="text-[10px] text-surface">{{ dialogs.mcpInspectionTools }}</span>
+ <span class="text-[10px] text-surface">{{ dialogs.mcpReadOnlyTools }}</span>
<AppSwitch
:model-value="inspectionToolsStatus.enabled"
:state="inspectionToolsStatus.state"
- :label="dialogs.mcpInspectionTools"
+ :label="dialogs.mcpReadOnlyTools"
data-test-id="settings-mcp-inspection-tools"
- @update:model-value="setMCPToolCategoryEnabled('inspect', $event)"
+ @update:model-value="setMCPToolCategoryEnabled('read', $event)"
/>
</div>
<div class="flex items-center justify-between gap-2 rounded bg-input px-2.5 py-2">
- <span class="text-[10px] text-surface">{{ dialogs.mcpModificationTools }}</span>
+ <span class="text-[10px] text-surface">{{ dialogs.mcpDocumentWritingTools }}</span>
<AppSwitch
:model-value="modificationToolsStatus.enabled"
:state="modificationToolsStatus.state"
- :label="dialogs.mcpModificationTools"
+ :label="dialogs.mcpDocumentWritingTools"
data-test-id="settings-mcp-modification-tools"
- @update:model-value="setMCPToolCategoryEnabled('modify', $event)"
+ @update:model-value="setMCPToolCategoryEnabled('write', $event)"
/>
</div>
</div>
diff --git a/tests/e2e/settings/credentials.spec.ts b/tests/e2e/settings/credentials.spec.ts
index 9af4acfaa..5d997e81c 100644
--- a/tests/e2e/settings/credentials.spec.ts
+++ b/tests/e2e/settings/credentials.spec.ts
@@ -98,20 +98,6 @@ test('MCP automation settings filter and persist tool availability', async ({ pa
await expect(page.getByTestId('settings-mcp-tool-create_shape')).toBeVisible()
await expect(page.getByTestId('settings-mcp-tool-get_page_tree')).toBeHidden()
- await search.fill('')
- const modificationTools = page.getByTestId('settings-mcp-modification-tools')
- await modificationTools.click()
- await expect(page.getByTestId('settings-mcp-tool-create_shape')).toHaveAttribute(
- 'data-state',
- 'unchecked'
- )
- await expect(page.getByTestId('settings-mcp-tool-get_page_tree')).toHaveAttribute(
- 'data-state',
- 'checked'
- )
- await modificationTools.click()
-
- await search.fill('create_shape')
await page.getByTestId('settings-mcp-tool-create_shape').click()
await page.reload()
await canvas.waitForInit()
diff --git a/tests/engine/app/automation/mcp-vite-plugin.test.ts b/tests/engine/app/automation/mcp-vite-plugin.test.ts
index 18cf9aa20..1cd31d99a 100644
--- a/tests/engine/app/automation/mcp-vite-plugin.test.ts
+++ b/tests/engine/app/automation/mcp-vite-plugin.test.ts
@@ -1,6 +1,10 @@
import { describe, expect, test } from 'bun:test'
+import { Readable } from 'node:stream'
-import { createAutomationEnvironment } from '@/app/automation/bridge/vite-plugin'
+import {
+ createAutomationEnvironment,
+ readDevMCPConfiguration
+} from '@/app/automation/bridge/vite-plugin'
describe('MCP Vite development server', () => {
test('passes an explicit empty auth token when authentication is disabled', () => {
@@ -20,4 +24,29 @@ describe('MCP Vite development server', () => {
expect(env.OPENPENCIL_MCP_ROOT).toBe('/designs')
expect(env.OPENPENCIL_MCP_DISABLED_TOOLS).toBe('eval,delete_node')
})
+
+ test('decodes UTF-8 configuration split across request chunks', async () => {
+ const body = Buffer.from(
+ JSON.stringify({
+ authenticationEnabled: true,
+ rootDirectory: '/设计',
+ disabledTools: ''
+ })
+ )
+ const split = body.indexOf(Buffer.from('设')) + 1
+ const request = Readable.from([body.subarray(0, split), body.subarray(split)])
+
+ await expect(readDevMCPConfiguration(request as never)).resolves.toEqual({
+ authenticationEnabled: true,
+ rootDirectory: '/设计',
+ disabledTools: ''
+ })
+ })
+
+ test('rejects configuration bodies above the byte limit', async () => {
+ const request = Readable.from([Buffer.alloc(70_001)])
+ await expect(readDevMCPConfiguration(request as never)).rejects.toThrow(
+ 'Request body is too large'
+ )
+ })
})