From 7c008e817375b25c3dbb196ff6c8d00195614db9 Mon Sep 17 00:00:00 2001 From: unfunny Date: Wed, 21 Oct 2026 09:15:57 -0400 Subject: [PATCH] Add MCP support in the IWA --- iwa-build.js | 11 ++- src/components/Editor.jsx | 201 +++++++++++++++++++++++++++++++++++++- src/mcp/client.js | 71 ++++++++++++++ vite.config.js | 6 ++ 4 files changed, 287 insertions(+), 2 deletions(-) create mode 100644 src/mcp/client.js diff --git a/iwa-build.js b/iwa-build.js index e2be6c5..112f3b5 100755 --- a/iwa-build.js +++ b/iwa-build.js @@ -37,7 +37,16 @@ try { sizes: icon.sizes, type: icon.type, purpose: 'any maskable' - })) + })), + permissions: [ + "*", + "tabs", + "storage" + ], + host_permissions: [ + "https://*/*", + "http://*/*" + ], }; // Write IWA manifest diff --git a/src/components/Editor.jsx b/src/components/Editor.jsx index 62bf628..75151f3 100644 --- a/src/components/Editor.jsx +++ b/src/components/Editor.jsx @@ -1,9 +1,208 @@ -import React, { useMemo } from 'react'; +import React, { useMemo, useState, useEffect } from 'react'; import MDEditor from '@uiw/react-md-editor'; import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; import katex from 'katex'; import 'katex/dist/katex.css'; +import { MCPClient } from '../mcp/client'; + +/** + * Custom Code component that handles: + * - Language "katex" or "math": live KaTeX math display + * - Language "svg": live SVG rendering + * - Standard code blocks with syntax highlighting + * + * + * + * @param {Object} props + * @param {Object} props.node + * @param {string} props.className + * @param {React.ReactNode} props.children + * @param {Object} props.props + */ +const CodeRenderer = ({ node, className, children, ...props }) => { + const codeString = String(children || '').replace(/\n$/, ''); + const match = /language-(\w+)/.exec(className || ''); + const language = match ? match[1].toLowerCase() : ''; + + // react-markdown v9+ removed the `inline` prop. Language-tagged fenced + // blocks always carry a `language-*` className, which is sufficient to + // distinguish the special KaTeX / SVG blocks from inline code. + if (language === 'katex' || language === 'math') { + try { + const html = katex.renderToString(codeString, { + displayMode: true, + throwOnError: false, + strict: false + }); + return ( +
+ ); + } catch (err) { + return ( +
+ Failed to render KaTeX: {err.message} +
+
+ SVG Preview +
+
+ {children} + + ); +}; + +/** + * Custom Image component that safely renders SVGs and normal images + * + * + * + * @param {Object} props + * @param {Object} props.node + * @param {string} props.src + * @param {string} props.alt + * @param {Object} props.props + */ +const ImageRenderer = ({ node, src, alt, ...props }) => { + const isSvg = src && (src.endsWith('.svg') || src.includes('.svg?') || src.startsWith('data:image/svg+xml')); + + return ( + + {alt { + e.target.style.opacity = '0.5'; + e.target.title = `Failed to load image: ${src}`; + }} + {...props} + /> + {alt && {alt}} + ({ + remarkPlugins: [remarkMath], + rehypePlugins: [[rehypeKatex, { throwOnError: false, strict: false }]], + components: { + code: CodeRenderer, + img: ImageRenderer, + } + }), []); + + // Handle drag and drop over the editor + const handleDragOver = (e) => { + e.preventDefault(); + e.stopPropagation(); + }; + + const handleDrop = (e) => { + e.preventDefault(); + e.stopPropagation(); + + const file = e.dataTransfer?.files?.[0]; + if (file && onFileDrop) { + onFileDrop(file); + } + }; + + const [mcpConfig, setMcpConfig] = useState({ address: '', port: '' }); + const [mcpConnected, setMcpConnected] = useState(false); + const [mcpStatus, setMcpStatus] = useState(''); + const mcpClient = useMemo(() => (typeof __IWA__ !== 'undefined' && __IWA__ ? new MCPClient() : null), []); + + useEffect(() => { + if (mcpClient && __IWA__ === 'true') { + // Optionally auto-connect or wait for user action + } + }, [mcpClient]); + + const connectMCP = async () => { + if (mcpClient && mcpConfig.address && mcpConfig.port) { + const success = await mcpClient.connect(mcpConfig.address, parseInt(mcpConfig.port)); + if (success) { + setMcpConnected(true); + setMcpStatus('Connected'); + } else { + setMcpStatus('Failed to connect'); + } + } + }; + + return ( +
+ + {__IWA__ === 'true' && ( +
+

MCP Connection (IWA Only)

+ setMcpConfig({ ...mcpConfig, address: e.target.value })} + style={{ marginRight: '10px' }} + /> + setMcpConfig({ ...mcpConfig, port: e.target.value })} + style={{ marginRight: '10px' }} + /> + +

Status: {mcpConnected ? 'Connected' : mcpStatus || 'Disconnected'}

+
+ )} +
{ + while (true) { + const { value, done } = await reader.read(); + if (done) break; + const message = new TextDecoder().decode(value); + console.log('Received from MCP:', message); + // Handle incoming JSON-RPC messages + } + })(); + } + + async sendRequest(method, params = {}) { + if (!this.isConnected) return; + + const request = { + jsonrpc: "2.0", + id: Date.now(), + method, + params + }; + + const encoder = new TextEncoder(); + const buffer = encoder.encode(JSON.stringify(request)); + const writer = this.writable.getWriter(); + await writer.write(buffer); + writer.releaseLock(); + } + + disconnect() { + if (this.socket) { + this.socket.close(); + this.isConnected = false; + } + } +} + diff --git a/vite.config.js b/vite.config.js index 0c87357..9172938 100644 --- a/vite.config.js +++ b/vite.config.js @@ -6,6 +6,12 @@ import { viteSingleFile } from 'vite-plugin-singlefile'; const singleFile = process.env.SINGLEFILE === '1'; const iwaBuild = process.env.IWA === '1'; +export default defineConfig({ + define: { + __IWA__: JSON.stringify(iwaBuild), + }, + plugins: [react(), ...(singleFile ? [viteSingleFile()] : [])], + export default defineConfig({ plugins: [react(), ...(singleFile ? [viteSingleFile()] : [])], server: {