MCPcopy Index your code
hub / github.com/coder/coder / startServer

Method startServer

cli/exp_mcp.go:675–771  ·  view source on GitHub ↗
(ctx context.Context, inv *serpent.Invocation, instructions string, allowedTools []string)

Source from the content-addressed store, hash-verified

673}
674
675func (s *mcpServer) startServer(ctx context.Context, inv *serpent.Invocation, instructions string, allowedTools []string) error {
676 cliui.Infof(inv.Stderr, "Starting MCP server")
677
678 cliui.Infof(inv.Stderr, "Instructions : %q", instructions)
679 if len(allowedTools) > 0 {
680 cliui.Infof(inv.Stderr, "Allowed Tools : %v", allowedTools)
681 }
682
683 // Capture the original stdin, stdout, and stderr.
684 invStdin := inv.Stdin
685 invStdout := inv.Stdout
686 invStderr := inv.Stderr
687 defer func() {
688 inv.Stdin = invStdin
689 inv.Stdout = invStdout
690 inv.Stderr = invStderr
691 }()
692
693 mcpSrv := server.NewMCPServer(
694 "Coder Agent",
695 buildinfo.Version(),
696 server.WithInstructions(instructions),
697 )
698
699 // If neither the user client nor the agent socket is available, there
700 // are no tools we can enable.
701 if s.client == nil && s.socketClient == nil {
702 return xerrors.New(notLoggedInMessage)
703 }
704
705 // Add tool dependencies.
706 toolOpts := []func(*toolsdk.Deps){
707 toolsdk.WithTaskReporter(func(args toolsdk.ReportTaskArgs) error {
708 state := codersdk.WorkspaceAppStatusState(args.State)
709 // The agent does not reliably report idle, so when AgentAPI is
710 // enabled we override idle to working and let the screen watcher
711 // detect the real idle via StatusStable. Final states (failure,
712 // complete) are trusted from the agent since the screen watcher
713 // cannot produce them.
714 if s.aiAgentAPIClient != nil && state == codersdk.WorkspaceAppStatusStateIdle {
715 state = codersdk.WorkspaceAppStatusStateWorking
716 }
717 return s.queue.Push(taskReport{
718 link: args.Link,
719 selfReported: true,
720 state: state,
721 summary: args.Summary,
722 })
723 }),
724 }
725
726 toolDeps, err := toolsdk.NewDeps(s.client, toolOpts...)
727 if err != nil {
728 return xerrors.Errorf("failed to initialize tool dependencies: %w", err)
729 }
730
731 // Register tools based on the allowlist. Zero length means allow everything.
732 for _, tool := range toolsdk.All {

Callers 1

mcpServerMethod · 0.95

Calls 13

InfofFunction · 0.92
VersionFunction · 0.92
WithTaskReporterFunction · 0.92
WorkspaceAppStatusStateTypeAlias · 0.92
NewDepsFunction · 0.92
WarnfFunction · 0.92
ErrorfFunction · 0.92
mcpFromSDKFunction · 0.70
NewMethod · 0.65
ListenMethod · 0.65
PushMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected