-
Notifications
You must be signed in to change notification settings - Fork 21
Refactor/vscode-extension-mcp-restructure #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nasdan
merged 4 commits into
feature/#812-implement-vscode-ext-and-mcp
from
refactor/vscode-extension-mcp-restructure
May 8, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c098d2
refactor(vscode-extension): restructure mcp module
Ivanruii 4f6612e
fix(vscode-extension): add TODO comment for implementing new wirefram…
Ivanruii df2e92c
refactor(vscode-extension): remove unused exports from mcp index
Ivanruii 87c2fb2
fix(vscode-extension): ensure all connections are closed when disposi…
Ivanruii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './new-wireframe'; | ||
| export * from './register'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import * as vscode from 'vscode'; | ||
|
|
||
| export const registerNewWireframeCommand = ( | ||
| context: vscode.ExtensionContext | ||
| ): void => { | ||
| context.subscriptions.push( | ||
| vscode.commands.registerCommand('quickmock.newWireframe', () => { | ||
| vscode.window.showInformationMessage('New wireframe coming soon'); // TODO: Implement the actual functionality for creating a new wireframe | ||
| }) | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import * as vscode from 'vscode'; | ||
| import { registerNewWireframeCommand } from './new-wireframe'; | ||
|
|
||
| /** | ||
| * Registers all VS Code commands exposed by the extension. | ||
| * @param context The VS Code extension context. | ||
| */ | ||
| export const registerCommands = (context: vscode.ExtensionContext): void => { | ||
| registerNewWireframeCommand(context); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import * as vscode from 'vscode'; | ||
|
|
||
| export const getPrimaryWorkspaceFolder = (): | ||
| | vscode.WorkspaceFolder | ||
| | undefined => vscode.workspace.workspaceFolders?.[0]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,15 @@ | ||
| import { logError, onAppUrlChange, syncAppUrlFile } from '#core'; | ||
| import { registerCommands } from '#commands'; | ||
| import { onAppUrlChange, syncAppUrlFile } from '#core'; | ||
| import { QuickMockEditorProvider } from '#editor'; | ||
| import { | ||
| registerMcpServer, | ||
| registerQuickMockMcpServerProvider, | ||
| RegistryServer, | ||
| } from '#mcp'; | ||
| import { setupMcp } from '#mcp'; | ||
| import * as vscode from 'vscode'; | ||
|
|
||
| export const activate = (context: vscode.ExtensionContext) => { | ||
| syncAppUrlFile(); | ||
| context.subscriptions.push(onAppUrlChange(syncAppUrlFile)); | ||
|
|
||
| context.subscriptions.push(QuickMockEditorProvider.register(context)); | ||
|
|
||
| const registryServer = new RegistryServer(); | ||
| registryServer | ||
| .start(context) | ||
| .catch(err => logError('Failed to start MCP registry server:', err)); | ||
|
|
||
| context.subscriptions.push(registerQuickMockMcpServerProvider(context)); | ||
|
|
||
| registerMcpServer(context).catch(err => | ||
| logError('Failed to register MCP server:', err) | ||
| ); | ||
|
|
||
| context.subscriptions.push( | ||
| vscode.commands.registerCommand('quickmock.newWireframe', () => { | ||
| vscode.window.showInformationMessage('New wireframe coming soon'); | ||
| }) | ||
| ); | ||
| setupMcp(context); | ||
| registerCommands(context); | ||
| }; | ||
|
|
||
| export const deactivate = () => {}; |
2 changes: 2 additions & 0 deletions
2
packages/vscode-extension/src/mcp/document-bridge/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const TOKEN_BYTE_LENGTH = 32; | ||
| export const PORT_FILE_MODE = 0o600; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './server'; |
94 changes: 94 additions & 0 deletions
94
packages/vscode-extension/src/mcp/document-bridge/server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { documentRegistry, getPrimaryWorkspaceFolder } from '#core'; | ||
| import { | ||
| buildPortFilePath, | ||
| DOCUMENT_ROUTE, | ||
| encodePortFile, | ||
| LOOPBACK_HOST, | ||
| TOKEN_HEADER, | ||
| } from '@lemoncode/quickmock-registry-protocol'; | ||
| import { randomBytes } from 'node:crypto'; | ||
| import { unlinkSync, writeFileSync } from 'node:fs'; | ||
| import { | ||
| createServer, | ||
| type IncomingMessage, | ||
| type ServerResponse, | ||
| } from 'node:http'; | ||
| import * as vscode from 'vscode'; | ||
| import { PORT_FILE_MODE, TOKEN_BYTE_LENGTH } from './constants'; | ||
|
|
||
| /** | ||
| * Starts the MCP document bridge server, which serves the content of documents open in the editor to the MCP server. | ||
| * @param context The VS Code extension context. | ||
| * @returns A promise that resolves when the server has started. | ||
| */ | ||
| export const startDocumentBridge = async ( | ||
| context: vscode.ExtensionContext | ||
| ): Promise<void> => { | ||
| const workspaceRoot = getPrimaryWorkspaceFolder()?.uri.fsPath; | ||
| if (!workspaceRoot) return; | ||
|
|
||
| const portFile = buildPortFilePath(workspaceRoot); | ||
| const token = randomBytes(TOKEN_BYTE_LENGTH).toString('hex'); | ||
|
|
||
| const handleRequest = (req: IncomingMessage, res: ServerResponse): void => { | ||
| if (req.headers[TOKEN_HEADER] !== token) { | ||
| res.writeHead(401); | ||
| res.end(); | ||
| return; | ||
| } | ||
|
|
||
| const url = new URL(req.url ?? '/', 'http://localhost'); | ||
|
|
||
| if (url.pathname !== DOCUMENT_ROUTE) { | ||
| res.writeHead(404); | ||
| res.end(); | ||
| return; | ||
| } | ||
|
|
||
| const path = url.searchParams.get('path'); | ||
| if (!path) { | ||
| res.writeHead(400); | ||
| res.end('Missing path parameter'); | ||
| return; | ||
| } | ||
|
|
||
| const content = documentRegistry.get(path); | ||
| if (content === undefined) { | ||
| res.writeHead(404); | ||
| res.end('Document not open in editor'); | ||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); | ||
| res.end(content); | ||
| }; | ||
|
|
||
| const server = createServer(handleRequest); | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| server.on('error', reject); | ||
| // Get the assigned port and write it to the port file | ||
| server.listen(0, LOOPBACK_HOST, () => { | ||
| const { port } = server.address() as { port: number }; | ||
| try { | ||
| writeFileSync(portFile, encodePortFile(port, token), { | ||
| mode: PORT_FILE_MODE, | ||
| }); | ||
| } catch (err) { | ||
| reject(err); | ||
| return; | ||
| } | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| context.subscriptions.push({ | ||
| dispose: () => { | ||
| server.closeAllConnections(); | ||
| server.close(); | ||
| try { | ||
| unlinkSync(portFile); | ||
| } catch {} | ||
| }, | ||
| }); | ||
| }; | ||
8 changes: 8 additions & 0 deletions
8
packages/vscode-extension/src/mcp/external-clients/clients/claude-code.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { homedir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import type { ExternalMcpClient } from '../model'; | ||
|
|
||
| export const claudeCode: ExternalMcpClient = { | ||
| label: 'Claude Code', | ||
| getConfigPath: () => join(homedir(), '.claude.json'), | ||
| }; |
4 changes: 4 additions & 0 deletions
4
packages/vscode-extension/src/mcp/external-clients/clients/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import type { ExternalMcpClient } from '../model'; | ||
| import { claudeCode } from './claude-code'; | ||
|
|
||
| export const externalClients: ExternalMcpClient[] = [claudeCode]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './model'; | ||
| export * from './register'; |
15 changes: 15 additions & 0 deletions
15
packages/vscode-extension/src/mcp/external-clients/model.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export interface ExternalMcpClient { | ||
| label: string; | ||
| getConfigPath: () => string; | ||
| } | ||
|
|
||
| export interface McpFileConfig { | ||
| mcpServers?: Record<string, unknown>; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| export interface McpServerEntry { | ||
| type: 'stdio'; | ||
| command: string; | ||
| args: string[]; | ||
| } |
65 changes: 65 additions & 0 deletions
65
packages/vscode-extension/src/mcp/external-clients/register.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { logError, logInfo } from '#core/logger'; | ||
| import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; | ||
| import { dirname } from 'node:path'; | ||
| import * as vscode from 'vscode'; | ||
| import { | ||
| getMcpInvocation, | ||
| MCP_SERVER_ID, | ||
| type McpInvocation, | ||
| } from '../invocation'; | ||
| import { externalClients } from './clients'; | ||
| import type { ExternalMcpClient, McpFileConfig, McpServerEntry } from './model'; | ||
|
|
||
| const readConfigFile = (path: string): McpFileConfig => { | ||
| try { | ||
| return JSON.parse(readFileSync(path, 'utf-8')) as McpFileConfig; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| }; | ||
|
|
||
| const writeConfigFile = (path: string, data: McpFileConfig): void => { | ||
| const dir = dirname(path); | ||
| if (!existsSync(dir)) mkdirSync(dir, { recursive: true }); | ||
| writeFileSync(path, JSON.stringify(data, null, 2), 'utf-8'); | ||
| }; | ||
|
|
||
| const buildMcpServerEntry = ({ | ||
| command, | ||
| args, | ||
| }: McpInvocation): McpServerEntry => ({ | ||
| type: 'stdio', | ||
| command, | ||
| args, | ||
| }); | ||
|
|
||
| const registerInClient = ( | ||
| client: ExternalMcpClient, | ||
| entry: McpServerEntry | ||
| ): void => { | ||
| const path = client.getConfigPath(); | ||
| if (!existsSync(path) && !existsSync(dirname(path))) return; | ||
|
|
||
| try { | ||
| const config = readConfigFile(path); | ||
| if (!config.mcpServers) config.mcpServers = {}; | ||
| config.mcpServers[MCP_SERVER_ID] = entry; | ||
| writeConfigFile(path, config); | ||
| logInfo(`MCP registered — ${client.label}`); | ||
| } catch (err) { | ||
| logError(`MCP registration failed — ${client.label}: ${String(err)}`); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Registers the MCP server configuration in external clients, such as ai assistants or tools that support MCP integration. | ||
| * @param context The VS Code extension context. | ||
| */ | ||
| export const registerExternalMcpClients = ( | ||
| context: vscode.ExtensionContext | ||
| ): void => { | ||
| const entry = buildMcpServerEntry(getMcpInvocation(context)); | ||
| for (const client of externalClients) { | ||
| registerInClient(client, entry); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1 @@ | ||
| export * from './mcp-client-targets'; | ||
| export * from './mcp-config-file'; | ||
| export * from './mcp-invocation'; | ||
| export * from './mcp-registration'; | ||
| export * from './registry-server'; | ||
| export * from './server-definition-provider'; | ||
| export * from './setup'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we only need to export the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const MCP_SERVER_ID = 'quickmock'; | ||
| export const MCP_PKG = '@lemoncode/quickmock-mcp'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './constants'; | ||
| export * from './invocation'; | ||
| export * from './model'; |
19 changes: 8 additions & 11 deletions
19
...scode-extension/src/mcp/mcp-invocation.ts → ...xtension/src/mcp/invocation/invocation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface McpInvocation { | ||
| command: string; | ||
| args: string[]; | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
server.close()stops accepting new connections but does not close existing ones, so any in-flight request at deactivation time is left hanging briefly.If the Node version allows it (18.2+), consider adding
server.closeAllConnections()beforeserver.close():There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. The minimum Node.js version for the minimum supported VS Code version is 22, so closeAllConnections is safe to use.