8000 feat: add status watcher to MCP server by code-asher · Pull Request #18320 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: add status watcher to MCP server #18320

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
merged 15 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only report user messages
  • Loading branch information
code-asher committed Jun 12, 2025
commit c8dc0dd7e94e347c331519bd15ff0cbea9e57d3b
34 changes: 19 additions & 15 deletions cli/exp_mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ func (r *RootCmd) mcpServer() *serpent.Command {
}

func (s *mcpServer) startReporter(ctx context.Context, inv *serpent.Invocation) {
// lastMessageID is the ID of the last *user* message that we saw. A user
// message only happens when interacting via the API (as opposed to
// interacting with the terminal directly).
var lastMessageID int64
shouldUpdate := func(item reportTask) codersdk.WorkspaceAppStatusState {
// Always send self-reported updates.
Expand All @@ -505,18 +508,17 @@ func (s *mcpServer) startReporter(ctx context.Context, inv *serpent.Invocation)
codersdk.WorkspaceAppStatusStateFailure:
return item.state
}
// Always send "working" when there is a new message, since this means the
// user submitted a message through the API and we know the LLM will begin
// work soon if it has not already.
// Always send "working" when there is a new user message, since we know the
// LLM will begin work soon if it has not already.
if item.messageID > lastMessageID {
return codersdk.WorkspaceAppStatusStateWorking
}
// Otherwise, if the state is "working" and there have been no new messages,
// it means either that the LLM is still working or it means the user has
// interacted with the terminal directly. For now, we are ignoring these
// updates. This risks missing cases where the user manually submits a new
// prompt and the LLM becomes active and does not update itself, but it
// avoids spamming useless status updates.
// Otherwise, if the state is "working" and there have been no new user
// messages, it means either that the LLM is still working or it means the
// user has interacted with the terminal directly. For now, we are ignoring
// these updates. This risks missing cases where the user manually submits
// a new prompt and the LLM becomes active and does not update itself, but
// it avoids spamming useless status updates.
return ""
}
var lastPayload agentsdk.PatchAppStatus
Expand Down Expand Up @@ -599,12 +601,14 @@ func (s *mcpServer) startWatcher(ctx context.Context, inv *serpent.Invocation) {
return
}
case agentapi.EventMessageUpdate:
err := s.queue.Push(reportTask{
messageID: ev.Id,
})
if err != nil {
cliui.Warnf(inv.Stderr, "Failed to queue update: %s", err)
return
if ev.Role == agentapi.RoleUser {
err := s.queue.Push(reportTask{
messageID: ev.Id,
})
if err != nil {
cliui.Warnf(inv.Stderr, "Failed to queue update: %s", err)
return
}
}
8000 }
case err := <-errCh:
Expand Down
25 changes: 15 additions & 10 deletions cli/exp_mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,12 @@ func TestExpMcpReporter(t *testing.T) {
}
}

makeMessageEvent := func(id int64) *codersdk.ServerSentEvent {
makeMessageEvent := func(id int64, role agentapi.ConversationRole) *codersdk.ServerSentEvent {
return &codersdk.ServerSentEvent{
Type: ServerSentEventTypeMessageUpdate,
Data: agentapi.EventMessageUpdate{
Id: id,
Id: id,
Role: role,
},
}
}
Expand All @@ -813,7 +814,7 @@ func TestExpMcpReporter(t *testing.T) {
return
}
// Send initial message.
send(*makeMessageEvent(0))
send(*makeMessageEvent(0, agentapi.RoleAgent))
listening <- send
<-closed
}))
Expand Down Expand Up @@ -902,13 +903,17 @@ func TestExpMcpReporter(t *testing.T) {
URI: "https://dev.coder.com",
},
},
// Terminal becomes active again according to the screen watcher, but
// message length is the same. This could be the LLM being active again,
// but it could also be the user messing around. We will prefer not
// updating the status so the "working" update here should be skipped.
// Terminal becomes active again according to the screen watcher, but no
// new user message. This could be the LLM being active again, but it
// could also be the user messing around. We will prefer not updating the
// status so the "working" update here should be skipped.
{
event: makeStatusEvent(agentapi.StatusRunning),
},
// Agent messages are ignored.
{
event: makeMessageEvent(1, agentapi.RoleAgent),
},
// LLM reports that it failed and URI is blank.
{
state: codersdk.WorkspaceAppStatusStateFailure,
Expand All @@ -923,10 +928,10 @@ func TestExpMcpReporter(t *testing.T) {
{
event: makeStatusEvent(agentapi.StatusRunning),
},
// ... but this time the message length has increased so we know there is
// LLM activity. This time the "working" update will not be skipped.
// ... but this time we have a new user message so we know there is LLM
// activity. This time the "working" update will not be skipped.
{
event: makeMessageEvent(1),
event: makeMessageEvent(2, agentapi.RoleUser),
expected: &codersdk.WorkspaceAppStatus{
State: codersdk.WorkspaceAppStatusStateWorking,
Message: "oops",
Expand Down
0