| 60 | } |
| 61 | |
| 62 | func (p *StreamableHTTPServerProxy) Init(ctx context.Context) (outErr error) { |
| 63 | ctx, span := p.tracer.Start(ctx, "StreamableHTTPServerProxy.Init", trace.WithAttributes(p.traceAttributes()...)) |
| 64 | defer tracing.EndSpanErr(span, &outErr) |
| 65 | |
| 66 | if err := p.client.Start(ctx); err != nil { |
| 67 | return xerrors.Errorf("start client: %w", err) |
| 68 | } |
| 69 | |
| 70 | version := mcp.LATEST_PROTOCOL_VERSION |
| 71 | initReq := mcp.InitializeRequest{ |
| 72 | Params: mcp.InitializeParams{ |
| 73 | ProtocolVersion: version, |
| 74 | ClientInfo: GetClientInfo(), |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | result, err := p.client.Initialize(ctx, initReq) |
| 79 | if err != nil { |
| 80 | return xerrors.Errorf("init MCP client: %w", err) |
| 81 | } |
| 82 | |
| 83 | if !slices.Contains(mcp.ValidProtocolVersions, result.ProtocolVersion) { |
| 84 | if err := p.client.Close(); err != nil { |
| 85 | p.logger.Debug(ctx, "failed to close MCP client on unsuccessful version negotiation", slog.Error(err)) |
| 86 | } |
| 87 | return xerrors.Errorf("MCP version negotiation failed; requested %q, accepts %q, received %q", version, strings.Join(mcp.ValidProtocolVersions, ","), result.ProtocolVersion) |
| 88 | } |
| 89 | |
| 90 | p.logger.Debug(ctx, "mcp client initialized", slog.F("name", result.ServerInfo.Name), slog.F("server_version", result.ServerInfo.Version)) |
| 91 | |
| 92 | tools, err := p.fetchTools(ctx) |
| 93 | if err != nil { |
| 94 | return xerrors.Errorf("fetch tools: %w", err) |
| 95 | } |
| 96 | |
| 97 | // Only include allowed tools. |
| 98 | p.tools = FilterAllowedTools(p.logger.Named("tool-filterer"), tools, p.allowlistPattern, p.denylistPattern) |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func (p *StreamableHTTPServerProxy) ListTools() []*Tool { |
| 103 | tools := maps.Values(p.tools) |