Welcome to the Computer Use project! This tutorial will guide you through building a system that allows an AI to control a computer just like a human does.
Imagine an AI model (like Claude) is a brilliant chef, and your computer is a kitchen filled with appliances (tools). The problem is, the chef is in another room and can't see what appliances you have or how to switch them on.
The MCP (Model Context Protocol) Server is the Waiters.
Before the AI can click a mouse or type text, it needs to know what tools are available.
The Goal: When the AI starts a session, the MCP Server scans your computer for installed applications and sends a menu of available tools to the AI.
Stdio (Standard Input/Output), meaning they talk via simple text messages in the terminal.computer_use").First, let's look at how we configure the system to recognize our MCP server. We need to tell the system, "Hey, use this specific code to handle computer commands."
We define the server configuration and list the tools we want to allow. This happens in setup.ts.
// From setup.ts
export function setupComputerUseMCP() {
// 1. Define the allowed tools (capabilities)
const allowedTools = buildComputerUseTools(
CLI_CU_CAPABILITIES,
getChicagoCoordinateMode()
).map(t => buildMcpToolName(COMPUTER_USE_MCP_SERVER_NAME, t.name))
// ... (setup continues below)
Explanation: Here we are building a list of "allowed tools." This is like printing the menu for the waiter to hand to the AI.
Next, we tell the system how to actually launch this server using standard input/output (stdio).
// From setup.ts
return {
mcpConfig: {
[COMPUTER_USE_MCP_SERVER_NAME]: {
type: 'stdio', // We talk via standard IO
command: process.execPath, // Run the current Node process
args: ['--computer-use-mcp'], // The flag to start server mode
scope: 'dynamic',
},
},
allowedTools,
}
}
Explanation: We create a configuration object. When the main application sees this, it knows to spawn a subprocess (a background worker) that runs our MCP server code.
What actually happens when the AI connects? The server doesn't just say "Hello"; it runs a quick diagnostic to see what apps are installed.
In the diagram above, the Host Adapter is the piece of code that actually touches the Operating System. We will build that in Host Adapter.
Let's look at mcpServer.ts to see how the server handles the "Inventory Check" without slowing down.
Scanning all apps on a computer can be slow. We don't want the AI to wait forever. We use a "race" to limit the time.
// From mcpServer.ts
async function tryGetInstalledAppNames(): Promise<string[] | undefined> {
const adapter = getComputerUseHostAdapter()
// 1. Start listing apps
const enumP = adapter.executor.listInstalledApps()
// 2. Start a 1-second timer
const timeoutP = new Promise<undefined>(resolve => {
setTimeout(resolve, 1000, undefined) // 1000ms timeout
})
Explanation: We start two races: asking the computer for apps (enumP) and a simple 1-second timer (timeoutP).
We wait to see who finishes first. If the computer is too slow, we just give up on the list so the server starts quickly.
// From mcpServer.ts
// 3. Race them!
const installed = await Promise.race([enumP, timeoutP])
.catch(() => undefined)
if (!installed) {
// If we timed out, log it and return nothing
return undefined
}
return filterAppsForDescription(installed, homedir())
}
Explanation: Promise.race picks the winner. If timeoutP wins, installed is undefined. The show must go on!
Finally, we create the server and attach our tool definitions.
// From mcpServer.ts
export async function createComputerUseMcpServerForCli() {
const adapter = getComputerUseHostAdapter()
const server = createComputerUseMcpServer(adapter, /*...*/)
// Get the apps (maybe undefined if too slow)
const installedAppNames = await tryGetInstalledAppNames()
// Register the handler for when AI asks "ListTools"
server.setRequestHandler(ListToolsRequestSchema, async () =>
adapter.isDisabled() ? { tools: [] } : { tools: /* build tools here */ }
)
return server
}
Explanation: This is the heart of the integration. We build the server, try to get the app list, and then tell the server: "When the AI asks for ListTools, give them this specific list we just built."
In this chapter, we learned:
Now that the AI knows what tools are available, it needs a way to actually use them (like moving the mouse or typing).
Next Chapter: The Executor (Computer Control)
Generated by Code IQ