@@ -2,14 +2,17 @@ package acpio
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
67 "io"
78 "log/slog"
9+ "os"
810 "strings"
911 "sync"
1012
1113 acp "github.com/coder/acp-go-sdk"
1214 st "github.com/coder/agentapi/lib/screentracker"
15+ "golang.org/x/xerrors"
1316)
1417
1518// Compile-time assertion that ACPAgentIO implements st.AgentIO
@@ -31,6 +34,10 @@ type acpClient struct {
3134 agentIO * ACPAgentIO
3235}
3336
37+ type McpConfig struct {
38+ McpServers []acp.McpServer `json:"mcpServers"`
39+ }
40+
3441var _ acp.Client = (* acpClient )(nil )
3542
3643func (c * acpClient ) SessionUpdate (ctx context.Context , params acp.SessionNotification ) error {
@@ -131,7 +138,7 @@ func (a *ACPAgentIO) SetOnChunk(fn func(chunk string)) {
131138}
132139
133140// NewWithPipes creates an ACPAgentIO connected via the provided pipes
134- func NewWithPipes (ctx context.Context , toAgent io.Writer , fromAgent io.Reader , logger * slog.Logger , getwd func () (string , error )) (* ACPAgentIO , error ) {
141+ func NewWithPipes (ctx context.Context , toAgent io.Writer , fromAgent io.Reader , logger * slog.Logger , getwd func () (string , error ), mcpFilePath string ) (* ACPAgentIO , error ) {
135142 if logger == nil {
136143 logger = slog .Default ()
137144 }
@@ -154,6 +161,12 @@ func NewWithPipes(ctx context.Context, toAgent io.Writer, fromAgent io.Reader, l
154161 }
155162 logger .Debug ("ACP initialized" , "protocolVersion" , initResp .ProtocolVersion )
156163
164+ // Prepare the MCPs for the session
165+ supportedMCPList , err := getSupportedMCPConfig (mcpFilePath , logger , & initResp )
166+ if err != nil {
167+ return nil , err
168+ }
169+
157170 // Create a session
158171 cwd , err := getwd ()
159172 if err != nil {
@@ -162,7 +175,7 @@ func NewWithPipes(ctx context.Context, toAgent io.Writer, fromAgent io.Reader, l
162175 }
163176 sessResp , err := conn .NewSession (ctx , acp.NewSessionRequest {
164177 Cwd : cwd ,
165- McpServers : []acp. McpServer {} ,
178+ McpServers : supportedMCPList ,
166179 })
167180 if err != nil {
168181 logger .Error ("Failed to create ACP session" , "error" , err )
@@ -174,6 +187,36 @@ func NewWithPipes(ctx context.Context, toAgent io.Writer, fromAgent io.Reader, l
174187 return agentIO , nil
175188}
176189
190+ func getSupportedMCPConfig (mcpFilePath string , logger * slog.Logger , initResp * acp.InitializeResponse ) ([]acp.McpServer , error ) {
191+ mcpFile , err := os .Open (mcpFilePath )
192+ if err != nil {
193+ return nil , xerrors .Errorf ("Failed to open mcp file: %v" , err )
194+ }
195+
196+ defer func () {
197+ if closeErr := mcpFile .Close (); closeErr != nil {
198+ logger .Error ("Failed to close mcp file" , "error" , err )
199+ }
200+ }()
201+
202+ var allMcpList McpConfig
203+ decoder := json .NewDecoder (mcpFile )
204+
205+ if err = decoder .Decode (& allMcpList ); err != nil {
206+ return nil , xerrors .Errorf ("Failed to decode mcp file: %v" , err )
207+ }
208+
209+ // Only send the MCPs that are supported by the agents
210+ var supportedMCPList []acp.McpServer
211+ for _ , mcp := range allMcpList .McpServers {
212+ if (mcp .Http != nil && ! initResp .AgentCapabilities .McpCapabilities .Http ) || (mcp .Sse != nil && ! initResp .AgentCapabilities .McpCapabilities .Sse ) {
213+ continue
214+ }
215+ supportedMCPList = append (supportedMCPList , mcp )
216+ }
217+ return supportedMCPList , err
218+ }
219+
177220// Write sends a message to the agent via ACP prompt
178221func (a * ACPAgentIO ) Write (data []byte ) (int , error ) {
179222 text := string (data )
0 commit comments