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.
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package toolsdk_test
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/codersdk/toolsdk"
|
|
)
|
|
|
|
func TestListOrganizations(t *testing.T) {
|
|
t.Parallel()
|
|
for _, count := range []int{0, 1, 2} {
|
|
t.Run(strconv.Itoa(count), func(t *testing.T) {
|
|
t.Parallel()
|
|
client, db := coderdtest.NewWithDatabase(t, nil)
|
|
owner := coderdtest.CreateFirstUser(t, client)
|
|
memberClient, member := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
|
org := dbgen.Organization(t, db, database.Organization{})
|
|
_ = dbgen.Organization(t, db, database.Organization{})
|
|
if count == 0 {
|
|
require.NoError(t, client.DeleteOrganizationMember(t.Context(), owner.OrganizationID, member.ID.String()))
|
|
}
|
|
if count == 2 {
|
|
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: org.ID, UserID: member.ID})
|
|
}
|
|
deps, err := toolsdk.NewDeps(memberClient)
|
|
require.NoError(t, err)
|
|
result, err := testTool(t, toolsdk.ListOrganizations, deps, toolsdk.NoArgs{})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, result)
|
|
require.Len(t, result, count)
|
|
expected, err := memberClient.OrganizationsByUser(t.Context(), codersdk.Me)
|
|
require.NoError(t, err)
|
|
require.ElementsMatch(t, expected, result)
|
|
for _, org := range result {
|
|
require.NotEqual(t, uuid.Nil, org.ID)
|
|
require.NotEmpty(t, org.Name)
|
|
require.NotEmpty(t, org.DisplayName)
|
|
}
|
|
})
|
|
}
|
|
}
|