Files
shumai/packages/dtos/src/mcp.ts
T
Yiling-J 851ae49c84 feat(mcp): support tool-level directTools array and 3-state UI configuration (#297)
* feat(mcp): support tool-level directTools array and 3-state UI configuration

* refactor(mcp): refine tool hint explanation and separate server instructions block

* style(mcp): display tool count in header title and split option hints into 3 lines

* style(mcp): update tools description paragraph and place bulk buttons on a separate row

* fix(mcp): enforce excludeTools in proxy path and align direct-tool counts

- Filter the per-agent tool registry by excludeTools at build time (DB cache
  and lazy-connect paths), so excluded tools are unreachable through the mcp
  proxy (call, search, list, describe) — matching pi-mcp-adapter where
  isToolAllowed gates both direct and gateway exposure.
- Add shared isDirectTool predicate (matches original or prefixed name) used
  by both buildDirectTools and the proxy description counts, so the
  advertised direct/proxy counts can no longer drift from actual
  registration; toolCount now reflects allowed tools only.
- Remove the legacy boolean directTools branch (no legacy data exists).
- Drop the now-unused mcp_excluded_tools_hint i18n key and regenerate
  Paraglide messages.
- Extend unit tests: excluded tools are not counted, listed, searched, or
  callable via the proxy; prefixed-name directTools configs count correctly.
2026-08-11 15:43:27 +08:00

138 lines
4.8 KiB
TypeScript

import { z } from 'zod'
export const mcpServerAuthTypeSchema = z.enum(['auto', 'none', 'bearer', 'oauth'])
export type McpServerAuthType = z.infer<typeof mcpServerAuthTypeSchema>
export const mcpTransportSchema = z.enum(['streamable_http', 'sse'])
export type McpTransport = z.infer<typeof mcpTransportSchema>
export const mcpServerPermissionSchema = z.enum(['owner', 'editor', 'reviewer'])
export type McpServerPermission = z.infer<typeof mcpServerPermissionSchema>
export const mcpOauthConfigSchema = z.object({
grantType: z.enum(['authorization_code', 'client_credentials']).optional(),
clientId: z.string().optional(),
clientSecret: z.string().optional(),
scope: z.string().optional(),
authorizationParams: z.record(z.string(), z.string()).optional(),
redirectUri: z.string().optional(),
clientName: z.string().optional(),
clientUri: z.string().optional(),
skipIssuerMetadataValidation: z.boolean().optional(),
})
export type McpOauthConfig = z.infer<typeof mcpOauthConfigSchema>
export const mcpServerAuthConfigSchema = z.object({
type: mcpServerAuthTypeSchema.optional(),
bearerToken: z.string().optional(),
headers: z.record(z.string(), z.string()).optional(),
oauth: mcpOauthConfigSchema.optional(),
})
export type McpServerAuthConfig = z.infer<typeof mcpServerAuthConfigSchema>
export const mcpServerConfigSchema = z.object({
excludeTools: z.array(z.string()).optional(),
requestTimeoutMs: z.number().int().positive().optional(),
idleTimeoutMs: z.number().int().positive().optional(),
keepAlive: z.boolean().optional(),
protocolVersion: z.enum(['legacy', 'auto', '2026-07-28']).optional(),
directTools: z.array(z.string()).optional(),
})
export type McpServerConfig = z.infer<typeof mcpServerConfigSchema>
export const mcpToolInfoSchema = z.object({
name: z.string(),
title: z.string().optional(),
description: z.string().optional(),
inputSchema: z.unknown().optional(),
})
export type McpToolInfo = z.infer<typeof mcpToolInfoSchema>
export const createMcpServerRequestSchema = z.object({
url: z.string().url(),
transport: mcpTransportSchema.optional(),
authConfig: mcpServerAuthConfigSchema.optional(),
config: mcpServerConfigSchema.optional(),
permission: mcpServerPermissionSchema.optional(),
})
export type CreateMcpServerRequest = z.infer<typeof createMcpServerRequestSchema>
export const updateMcpServerRequestSchema = z.object({
transport: mcpTransportSchema.optional(),
authConfig: mcpServerAuthConfigSchema.optional(),
config: mcpServerConfigSchema.optional(),
permission: mcpServerPermissionSchema.optional(),
})
export type UpdateMcpServerRequest = z.infer<typeof updateMcpServerRequestSchema>
export const updateMcpServerPermissionRequestSchema = z.object({
permission: mcpServerPermissionSchema,
})
export type UpdateMcpServerPermissionRequest = z.infer<
typeof updateMcpServerPermissionRequestSchema
>
export const mcpServerInfoSchema = z.object({
id: z.string(),
name: z.string(),
description: z.string().optional(),
instructions: z.string().optional(),
url: z.string(),
transport: mcpTransportSchema,
authType: mcpServerAuthTypeSchema,
config: mcpServerConfigSchema.optional(),
permission: mcpServerPermissionSchema,
status: z.string(),
lastError: z.string().nullable().optional(),
toolCount: z.number(),
hasCredential: z.boolean(),
createdAt: z.string(),
updatedAt: z.string(),
})
export type McpServerInfo = z.infer<typeof mcpServerInfoSchema>
export const listMcpServersResponseSchema = z.object({
servers: z.array(mcpServerInfoSchema),
})
export type ListMcpServersResponse = z.infer<typeof listMcpServersResponseSchema>
export const refreshToolsRequestSchema = z.object({})
export type RefreshToolsRequest = z.infer<typeof refreshToolsRequestSchema>
export const refreshToolsResponseSchema = z.object({
tools: z.array(mcpToolInfoSchema),
})
export type RefreshToolsResponse = z.infer<typeof refreshToolsResponseSchema>
export const mcpAuthStatusSchema = z.enum([
'authenticated',
'expired',
'not_authenticated',
'in_progress',
])
export type McpAuthStatus = z.infer<typeof mcpAuthStatusSchema>
export const mcpAuthStatusResponseSchema = z.object({
status: mcpAuthStatusSchema,
})
export type McpAuthStatusResponse = z.infer<typeof mcpAuthStatusResponseSchema>
export const mcpAuthStartResponseSchema = z.object({
authorizationUrl: z.string().optional(),
status: mcpAuthStatusSchema,
})
export type McpAuthStartResponse = z.infer<typeof mcpAuthStartResponseSchema>
export const mcpAuthCompleteRequestSchema = z.object({
code: z.string(),
iss: z.string().optional(),
})
export type McpAuthCompleteRequest = z.infer<typeof mcpAuthCompleteRequestSchema>
export const mcpTestServerResponseSchema = z.object({
ok: z.boolean(),
toolCount: z.number(),
message: z.string().optional(),
})
export type McpTestServerResponse = z.infer<typeof mcpTestServerResponseSchema>