Files
coder/codersdk/toolsdk/organizations.go
T
Hugo Dutka 8e3c36ec5a fix: standardize org use in the mcp server tools (#29141)
Addresses
https://linear.app/codercom/issue/CODAGT-970/expose-mcp-tools-for-organization-selection.

Currently our MCP server does not handle organizations consistently.
Some tools, like `create_template`, use a random organization if the
user is in multiple orgs. Others, like `create_chat`, pick the
organization which contains the latest chat the user spawned.

This PR standardizes organization use in the MCP server tools.
Organization ID is now in the argument schema of all tools that depend
on it. It's always optional. If the user is only a member of one
organization, tools default to that organization. If the user is a
member of multiple organizations, tools return an error asking to supply
an organization id.

This PR also adds a new `list_organizations` tool to allow MCP clients
to view the organizations that the user has membership in.
2026-09-10 11:48:08 +02:00

60 lines
2.0 KiB
Go

package toolsdk
import (
"context"
"github.com/google/uuid"
"golang.org/x/xerrors"
"github.com/coder/aisdk-go"
"github.com/coder/coder/v2/codersdk"
)
// ListOrganizations lists the authenticated user's visible memberships.
var ListOrganizations = Tool[NoArgs, []codersdk.Organization]{
Tool: aisdk.Tool{
Name: ToolNameListOrganizations,
Description: "List organizations the authenticated user belongs to, including IDs, names, and display names. Membership does not grant permission to perform every organization-scoped action.",
Schema: aisdk.Schema{
Properties: map[string]any{},
Required: []string{},
},
},
MCPAnnotations: mcpReadOnlyAnnotations,
Handler: func(ctx context.Context, deps Deps, _ NoArgs) ([]codersdk.Organization, error) {
organizations, err := deps.coderClient.OrganizationsByUser(ctx, codersdk.Me)
if err != nil {
return nil, xerrors.Errorf("list organizations: %w", err)
}
if organizations == nil {
organizations = []codersdk.Organization{}
}
return organizations, nil
},
}
func resolveOrganization(ctx context.Context, deps Deps, organizationID string) (uuid.UUID, error) {
if organizationID != "" {
id, err := uuid.Parse(organizationID)
if err != nil {
return uuid.Nil, xerrors.New("organization_id must be a valid UUID")
}
if id == uuid.Nil {
return uuid.Nil, xerrors.New("organization_id must be a valid nonzero UUID")
}
return id, nil
}
me, err := deps.coderClient.User(ctx, codersdk.Me)
if err != nil {
return uuid.Nil, xerrors.Errorf("resolve organization: get authenticated user: %w", err)
}
switch len(me.OrganizationIDs) {
case 0:
return uuid.Nil, xerrors.Errorf("organization_id is required because you belong to no organizations; use %s to inspect organization membership details", ToolNameListOrganizations)
case 1:
return me.OrganizationIDs[0], nil
default:
return uuid.Nil, xerrors.Errorf("organization_id is required because you belong to multiple organizations; use %s to obtain organization IDs and details", ToolNameListOrganizations)
}
}