Documentation
¶
Overview ¶
Copyright 2025 Google LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Constants
- func GetCloudSQLOpts(ipType, userAgent string, useIAM bool) ([]cloudsqlconn.Option, error)
- func GetIAMAccessToken(ctx context.Context) (string, error)
- func GetIAMPrincipalEmailFromADC(ctx context.Context, dbType string) (string, error)
- func InitConnectionSpan(ctx context.Context, tracer trace.Tracer, sourceType, sourceName string) (context.Context, trace.Span)
- func NewReloadingDefaultCredentials(ctx context.Context, p ADCReloadParams) (*google.Credentials, error)
- func NormalizeValue(val any, oid uint32) any
- func Register(sourceType string, factory SourceConfigFactory) bool
- type ADCReloadParams
- type Cache
- type ConnectOnce
- type Dialect
- type IPType
- type Item
- type OAuthProxyCapabilities
- type OAuthProxySource
- type OnEvictFunc
- type Option
- type Source
- type SourceConfig
- type SourceConfigFactory
Constants ¶
const CloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform"
CloudPlatformScope is the OAuth2 scope for Google Cloud Platform services.
const ConnectTimeout = 60 * time.Second
ConnectTimeout is the default ceiling on a connection attempt. It is sized for a cold cloud connector path rather than for a healthy connection.
Variables ¶
This section is empty.
Functions ¶
func GetCloudSQLOpts ¶
func GetCloudSQLOpts(ipType, userAgent string, useIAM bool) ([]cloudsqlconn.Option, error)
GetCloudSQLDialOpts retrieve dial options with the right ip type and user agent for cloud sql databases.
func GetIAMPrincipalEmailFromADC ¶
GetIAMPrincipalEmailFromADC finds the email associated with ADC
func InitConnectionSpan ¶
func InitConnectionSpan(ctx context.Context, tracer trace.Tracer, sourceType, sourceName string) (context.Context, trace.Span)
InitConnectionSpan adds a span for database pool connection initialization
func NewReloadingDefaultCredentials ¶ added in v1.16.0
func NewReloadingDefaultCredentials(ctx context.Context, p ADCReloadParams) (*google.Credentials, error)
NewReloadingDefaultCredentials finds Application Default Credentials and wraps their TokenSource so rotated on-disk credentials are reloaded automatically on mint failure.
func NormalizeValue ¶ added in v1.10.0
NormalizeValue converts specific database types to friendly representations (e.g. UUID to string).
func Register ¶
func Register(sourceType string, factory SourceConfigFactory) bool
Register registers a new source type with its factory. It returns false if the type is already registered.
Types ¶
type ADCReloadParams ¶ added in v1.16.0
type ADCReloadParams struct {
Name string
Scopes []string
QuotaProjectPin string
SkipQuotaProjectGuard bool
}
ADCReloadParams configures NewReloadingDefaultCredentials.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe, expiring key-value store
func NewCache ¶
func NewCache(onEvict OnEvictFunc) *Cache
NewCache creates a new cache and cleans up every 55 min
func (*Cache) DeleteExpired ¶
func (c *Cache) DeleteExpired()
DeleteExpired removes all expired items
type ConnectOnce ¶ added in v1.11.0
type ConnectOnce[T any] struct { // contains filtered or unexported fields }
ConnectOnce holds a connection a source builds on first use. A source keeps one of these instead of a bare handle and resolves it through Do.
func NewConnectOnce ¶ added in v1.11.0
func NewConnectOnce[T any](ctx context.Context, name, sourceType string, tracer trace.Tracer, opts ...Option) *ConnectOnce[T]
NewConnectOnce returns a holder for a connection that has not been made yet. ctx must be the context Initialize was called with: every later connect runs under it, so the source reports the startup user agent and cannot pick up request-scoped values from whichever caller happens to trigger it.
func (*ConnectOnce[T]) Close ¶ added in v1.11.0
func (c *ConnectOnce[T]) Close(ctx context.Context) error
Close releases the connection, if one was ever made, and stops another from being made. It is safe on a source that never connected and safe to call twice.
Close does not wait for an attempt that is already in flight. That attempt releases its own result rather than caching it into a closed holder, so shutdown never blocks on a connect that may be hung for the full timeout.
func (*ConnectOnce[T]) Do ¶ added in v1.11.0
func (c *ConnectOnce[T]) Do(ctx context.Context, connect func(context.Context) (T, error)) (T, error)
Do returns the connection, making it on the first call. Concurrent callers share one attempt, and a failed attempt is not remembered.
Because a failure is retried by the next caller rather than cached, connect must release whatever it had already built before it returns an error. A pool that fails its ping and is returned unclosed leaks once per tool call, not once per process.
func (*ConnectOnce[T]) Get ¶ added in v1.11.0
func (c *ConnectOnce[T]) Get() (T, bool)
Get returns the connection if one has already been made. It never blocks and never fails, so a source's context-free accessors — the ones tools type assert on — can report a handle without being able to build one.
func (*ConnectOnce[T]) OnClose ¶ added in v1.11.0
func (c *ConnectOnce[T]) OnClose(fn func(context.Context, T) error) *ConnectOnce[T]
OnClose registers how to release the connection. A source that holds a handle with no teardown can leave it unset. It is meant to be chained onto NewConnectOnce, before the holder is reachable by another goroutine.
type OAuthProxyCapabilities ¶ added in v1.17.0
type OAuthProxyCapabilities interface {
OAuthResponseTypesSupported() []string
OAuthGrantTypesSupported() []string
OAuthCodeChallengeMethodsSupported() []string
}
OAuthProxyCapabilities can optionally be implemented by an OAuthProxySource to override the default OAuth capabilities advertised in its proxied discovery document (response_types_supported: ["code"], grant_types_supported: ["authorization_code", "refresh_token"], code_challenge_methods_supported: ["S256"]). Most authorization servers matching OAuthProxySource use these defaults and don't need to implement this.
type OAuthProxySource ¶ added in v1.17.0
type OAuthProxySource interface {
Source
// ClientOAuthEnabled reports whether this source instance expects the
// caller to supply a per-request, browser-obtained OAuth access token,
// as opposed to a single credential configured for the whole process.
ClientOAuthEnabled() bool
// OAuthAuthorizationEndpoint returns the full URL of the authorization
// server's authorization endpoint (where the browser is redirected to
// authenticate). This is returned as-is in the proxied discovery
// document: the initial redirect happens via top-level browser
// navigation, which isn't subject to CORS, so it does not need to be
// proxied.
OAuthAuthorizationEndpoint() string
// OAuthTokenEndpoint returns the full URL of the authorization server's
// own token endpoint. The toolbox's token proxy forwards requests here
// server-side; browser clients never call this URL directly.
OAuthTokenEndpoint() string
}
OAuthProxySource is implemented by a Source whose OAuth authorization server does not send CORS headers on its discovery document or token endpoint. Browsers enforce CORS on both fetch/XHR requests, so a browser-based MCP client cannot complete an OAuth Authorization Code + PKCE flow directly against such a server, even though the same requests work fine from a non-browser HTTP client (curl, a backend service, etc).
A Source that implements this interface, and reports ClientOAuthEnabled() == true, has its OAuth discovery document and token endpoint proxied by the toolbox itself (see internal/server/oauth_proxy.go), at:
GET /.well-known/oauth-authorization-server/oauth-proxy/{sourceName}
POST /oauth-proxy/{sourceName}/token
The browser then only ever talks to the toolbox's own origin, which already sends the CORS headers configured via --allowed-origins, and the toolbox forwards the token exchange to the authorization server server-side, where CORS does not apply.
A source supplies its own full endpoint URLs rather than a bare base URL plus an assumed path suffix, because different authorization servers use different conventions and the toolbox should not need to guess any source's URL layout.
type OnEvictFunc ¶
OnEvictFunc is the signature for the callback
type Option ¶ added in v1.11.0
type Option func(*options)
Option configures a ConnectOnce.
func WithMinConnectTimeout ¶ added in v1.11.0
WithMinConnectTimeout raises the ceiling for a source whose own configuration allows a longer connect. A shorter value is ignored: the source's own bound still applies inside the ceiling, and lowering the ceiling would not make it any tighter.
type Source ¶
type Source interface {
SourceType() string
ToConfig() SourceConfig
IsReadOnly() bool
}
Source is the interface for the source itself.
type SourceConfig ¶
type SourceConfig interface {
SourceConfigType() string
Initialize(ctx context.Context, tracer trace.Tracer) (Source, error)
}
SourceConfig is the interface for configuring a source.
func DecodeConfig ¶
func DecodeConfig(ctx context.Context, sourceType string, name string, decoder *yaml.Decoder) (SourceConfig, error)
DecodeConfig decodes a source configuration using the registered factory for the given type.
type SourceConfigFactory ¶
type SourceConfigFactory func(ctx context.Context, name string, decoder *yaml.Decoder) (SourceConfig, error)
SourceConfigFactory defines the function signature for creating a SourceConfig.