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

Method Init

aibridge/mcp/proxy_streamable_http.go:62–100  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

60}
61
62func (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
102func (p *StreamableHTTPServerProxy) ListTools() []*Tool {
103 tools := maps.Values(p.tools)

Callers 1

TestToolInjectionOrderFunction · 0.95

Calls 12

traceAttributesMethod · 0.95
fetchToolsMethod · 0.95
EndSpanErrFunction · 0.92
GetClientInfoFunction · 0.85
FilterAllowedToolsFunction · 0.85
InitializeMethod · 0.80
NamedMethod · 0.80
StartMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.45
ContainsMethod · 0.45
ErrorMethod · 0.45

Tested by 1

TestToolInjectionOrderFunction · 0.76