fix(claude-code): run post-install script after configuring MCP and remove send-keys workaround (#130)

- needed to install MCP servers
- workaround sucked

---------

Co-authored-by: DevelopmentCats <christofer@coder.com>
This commit is contained in:
Ben Potter 2025-06-02 18:37:48 -05:00 committed by GitHub
parent 13a25ff4af
commit 9e7ce393c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 22 deletions

View File

@ -14,7 +14,7 @@ Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude
```tf ```tf
module "claude-code" { module "claude-code" {
source = "registry.coder.com/coder/claude-code/coder" source = "registry.coder.com/coder/claude-code/coder"
version = "1.3.0" version = "1.3.1"
agent_id = coder_agent.example.id agent_id = coder_agent.example.id
folder = "/home/coder" folder = "/home/coder"
install_claude_code = true install_claude_code = true
@ -88,7 +88,7 @@ resource "coder_agent" "main" {
module "claude-code" { module "claude-code" {
count = data.coder_workspace.me.start_count count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/claude-code/coder" source = "registry.coder.com/coder/claude-code/coder"
version = "1.1.0" version = "1.3.1"
agent_id = coder_agent.example.id agent_id = coder_agent.example.id
folder = "/home/coder" folder = "/home/coder"
install_claude_code = true install_claude_code = true
@ -107,7 +107,7 @@ Run Claude Code as a standalone app in your workspace. This will install Claude
```tf ```tf
module "claude-code" { module "claude-code" {
source = "registry.coder.com/coder/claude-code/coder" source = "registry.coder.com/coder/claude-code/coder"
version = "1.3.0" version = "1.3.1"
agent_id = coder_agent.example.id agent_id = coder_agent.example.id
folder = "/home/coder" folder = "/home/coder"
install_claude_code = true install_claude_code = true

View File

@ -131,6 +131,11 @@ resource "coder_script" "claude_code" {
npm install -g @anthropic-ai/claude-code@${var.claude_code_version} npm install -g @anthropic-ai/claude-code@${var.claude_code_version}
fi fi
if [ "${var.experiment_report_tasks}" = "true" ]; then
echo "Configuring Claude Code to report tasks via Coder MCP..."
coder exp mcp configure claude-code ${var.folder}
fi
# Run post-install script if provided # Run post-install script if provided
if [ -n "${local.encoded_post_install_script}" ]; then if [ -n "${local.encoded_post_install_script}" ]; then
echo "Running post-install script..." echo "Running post-install script..."
@ -139,11 +144,6 @@ resource "coder_script" "claude_code" {
/tmp/post_install.sh /tmp/post_install.sh
fi fi
if [ "${var.experiment_report_tasks}" = "true" ]; then
echo "Configuring Claude Code to report tasks via Coder MCP..."
coder exp mcp configure claude-code ${var.folder}
fi
# Handle terminal multiplexer selection (tmux or screen) # Handle terminal multiplexer selection (tmux or screen)
if [ "${var.experiment_use_tmux}" = "true" ] && [ "${var.experiment_use_screen}" = "true" ]; then if [ "${var.experiment_use_tmux}" = "true" ] && [ "${var.experiment_use_screen}" = "true" ]; then
echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously." echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously."
@ -167,14 +167,8 @@ resource "coder_script" "claude_code" {
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
# Create a new tmux session in detached mode # Create a new tmux session in detached mode
tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions" tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions \"$CODER_MCP_CLAUDE_TASK_PROMPT\""
# Send the prompt to the tmux session if needed
if [ -n "$CODER_MCP_CLAUDE_TASK_PROMPT" ]; then
tmux send-keys -t claude-code "$CODER_MCP_CLAUDE_TASK_PROMPT"
sleep 5
tmux send-keys -t claude-code Enter
fi
fi fi
# Run with screen if enabled # Run with screen if enabled
@ -209,15 +203,9 @@ resource "coder_script" "claude_code" {
screen -U -dmS claude-code bash -c ' screen -U -dmS claude-code bash -c '
cd ${var.folder} cd ${var.folder}
claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log" claude --dangerously-skip-permissions "$CODER_MCP_CLAUDE_TASK_PROMPT" | tee -a "$HOME/.claude-code.log"
exec bash exec bash
' '
# Extremely hacky way to send the prompt to the screen session
# This will be fixed in the future, but `claude` was not sending MCP
# tasks when an initial prompt is provided.
screen -S claude-code -X stuff "$CODER_MCP_CLAUDE_TASK_PROMPT"
sleep 5
screen -S claude-code -X stuff "^M"
else else
# Check if claude is installed before running # Check if claude is installed before running
if ! command_exists claude; then if ! command_exists claude; then