BS
BleepingSwift
Published on
4 min read

> Auto-Approve the Xcode MCP Connection Prompt with Claude Code Hooks

If you've set up Xcode's MCP server to give Claude Code access to your project structure and build tools, you've probably noticed an annoying pattern. Every time you restart Claude Code, or even just run /clear to start a fresh conversation, Xcode pops up a dialog asking if you want to allow the connection. You have to click over to Xcode, find the prompt, click "Allow", and switch back to your terminal.

This gets old fast, especially during active development where you might clear your session multiple times an hour. The good news is you can automate this using Claude Code's hook system and a bit of JavaScript for Automation.

How Hooks Work

Claude Code supports user-configurable hooks that run shell commands in response to specific events. One of these events is SessionStart, which fires when you begin a new session or clear your current one. By hooking into this event, you can run a script that finds and dismisses the Xcode permission dialog automatically.

The Auto-Approve Script

Create a file called allow_mcp.js somewhere convenient (I keep mine in ~/.claude/scripts/):

#!/usr/bin/env osascript -l JavaScript

function run() {
  const xcode = Application('Xcode')
  if (!xcode.running()) {
    return 'Xcode not running'
  }

  const systemEvents = Application('System Events')
  const xcodeProcess = systemEvents.processes.byName('Xcode')

  let approvedCount = 0

  try {
    const windows = xcodeProcess.windows()
    for (const window of windows) {
      // Look for dialog windows asking about MCP access
      const staticTexts = window.staticTexts()
      for (const text of staticTexts) {
        if (text.value().includes('to access Xcode?')) {
          // Find and click the Allow button
          const buttons = window.buttons()
          for (const button of buttons) {
            if (button.name() === 'Allow') {
              button.click()
              approvedCount++
              break
            }
          }
        }
      }
    }
  } catch (e) {
    return 'Error: ' + e.message
  }

  return approvedCount > 0
    ? `Approved ${approvedCount} MCP connection(s)`
    : 'No pending MCP dialogs'
}

Make the script executable:

chmod +x ~/.claude/scripts/allow_mcp.js

Granting Accessibility Permissions

The script uses System Events to interact with Xcode's UI, which requires accessibility permissions. The first time it runs, macOS will prompt you to grant access. You can also add this manually:

  1. Open System Settings > Privacy & Security > Accessibility
  2. Add your terminal app (Terminal, iTerm2, Warp, etc.) to the list

Without these permissions, the script won't be able to click the Allow button.

Configuring the Hook

Add the hook to your Claude Code settings. Open ~/.claude/settings.json (create it if it doesn't exist) and add:

{
  "hooks": {
    "SessionStart": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/scripts/allow_mcp.js",
            "timeout": 10000
          }
        ]
      }
    ]
  }
}

The timeout gives the script up to 10 seconds to complete, which is plenty of time for the UI automation to run.

Testing It Out

Restart Claude Code or run /clear in an existing session. If Xcode shows the MCP connection dialog, the hook should automatically dismiss it within a second or two. You can verify it's working by watching the Xcode window when you start a new session.

If you want to test the script manually:

osascript ~/.claude/scripts/allow_mcp.js

This will output either "Approved 1 MCP connection(s)" or "No pending MCP dialogs" depending on whether a prompt was visible.

Why This Works

The script uses JavaScript for Automation (JXA) to interact with System Events. It iterates through Xcode's open windows looking for dialog text containing "to access Xcode?" and then programmatically clicks the Allow button. Because it runs on every session start, it catches the dialog right when it appears.

One thing to note: this only auto-approves MCP connections when Xcode is already running. If Xcode isn't open, the script returns early and does nothing. You'll still need to manually approve the first connection when launching Xcode fresh.

Security Considerations

Automatically approving these dialogs does bypass the manual confirmation step that Xcode provides. The MCP server only accepts local connections, so the risk is limited to processes running on your machine. If you're working on sensitive code and want to maintain manual control over which tools can access Xcode, you might prefer living with the prompts.

For most development workflows though, if you've already decided to grant Claude Code access to your project via MCP, automating the approval just removes friction from the experience.

Credit to oronbz for the original implementation of this approach.

subscribe.sh

// Stay Updated

Get notified when I publish new tutorials on Swift, SwiftUI, and iOS development. No spam, unsubscribe anytime.

>

By subscribing, you agree to our Privacy Policy.