feat: Enhance MCP service configuration with stdio transport support
- Added support for stdio transport type in MCP service configuration, allowing command execution via standard input/output. - Updated MCPService interface to include optional stdio_config and env_vars fields for managing command arguments and environment variables. - Enhanced frontend components to handle stdio transport, including dynamic form fields for command and arguments. - Implemented validation logic for stdio configuration in the service update process, ensuring proper handling of required fields. - Updated backend logic to manage stdio transport connections and ensure proper client lifecycle management.
This commit is contained in:
@@ -6,8 +6,8 @@ export interface MCPService {
|
||||
name: string
|
||||
description: string
|
||||
enabled: boolean
|
||||
transport_type: 'sse' | 'http-streamable'
|
||||
url: string
|
||||
transport_type: 'sse' | 'http-streamable' | 'stdio'
|
||||
url?: string // Optional: required for SSE/HTTP Streamable
|
||||
headers?: Record<string, string>
|
||||
auth_config?: {
|
||||
api_key?: string
|
||||
@@ -19,6 +19,11 @@ export interface MCPService {
|
||||
retry_count?: number
|
||||
retry_delay?: number
|
||||
}
|
||||
stdio_config?: {
|
||||
command: 'uvx' | 'npx' // Command: uvx or npx
|
||||
args: string[] // Command arguments array
|
||||
}
|
||||
env_vars?: Record<string, string> // Environment variables for stdio transport
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
@@ -36,11 +36,11 @@
|
||||
<div class="service-name">
|
||||
{{ service.name }}
|
||||
<t-tag
|
||||
:theme="service.transport_type === 'sse' ? 'success' : 'primary'"
|
||||
:theme="getTransportTypeTheme(service.transport_type)"
|
||||
size="small"
|
||||
variant="light"
|
||||
>
|
||||
{{ service.transport_type === 'sse' ? 'SSE' : 'HTTP Streamable' }}
|
||||
{{ getTransportTypeLabel(service.transport_type) }}
|
||||
</t-tag>
|
||||
</div>
|
||||
<div class="service-controls">
|
||||
@@ -274,6 +274,34 @@ const handleMenuAction = (data: { value: string }, service: MCPService) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Get transport type theme for tag
|
||||
const getTransportTypeTheme = (transportType: string) => {
|
||||
switch (transportType) {
|
||||
case 'sse':
|
||||
return 'success'
|
||||
case 'http-streamable':
|
||||
return 'primary'
|
||||
case 'stdio':
|
||||
return 'warning'
|
||||
default:
|
||||
return 'default'
|
||||
}
|
||||
}
|
||||
|
||||
// Get transport type label
|
||||
const getTransportTypeLabel = (transportType: string) => {
|
||||
switch (transportType) {
|
||||
case 'sse':
|
||||
return 'SSE'
|
||||
case 'http-streamable':
|
||||
return 'HTTP Streamable'
|
||||
case 'stdio':
|
||||
return 'Stdio'
|
||||
default:
|
||||
return transportType
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadServices()
|
||||
})
|
||||
|
||||
@@ -29,13 +29,101 @@
|
||||
<t-radio-group v-model="formData.transport_type">
|
||||
<t-radio value="sse">SSE (Server-Sent Events)</t-radio>
|
||||
<t-radio value="http-streamable">HTTP Streamable</t-radio>
|
||||
<t-radio value="stdio">Stdio</t-radio>
|
||||
</t-radio-group>
|
||||
</t-form-item>
|
||||
|
||||
<t-form-item label="服务 URL" name="url">
|
||||
<!-- URL for SSE/HTTP Streamable -->
|
||||
<t-form-item
|
||||
v-if="formData.transport_type !== 'stdio'"
|
||||
label="服务 URL"
|
||||
name="url"
|
||||
>
|
||||
<t-input v-model="formData.url" placeholder="https://example.com/mcp" />
|
||||
</t-form-item>
|
||||
|
||||
<!-- Stdio Config -->
|
||||
<template v-if="formData.transport_type === 'stdio'">
|
||||
<t-form-item label="命令" name="stdio_config.command">
|
||||
<t-radio-group v-model="formData.stdio_config.command">
|
||||
<t-radio value="uvx">uvx</t-radio>
|
||||
<t-radio value="npx">npx</t-radio>
|
||||
</t-radio-group>
|
||||
</t-form-item>
|
||||
|
||||
<t-form-item label="参数" name="stdio_config.args">
|
||||
<div class="args-input-container">
|
||||
<div
|
||||
v-for="(arg, index) in formData.stdio_config.args"
|
||||
:key="index"
|
||||
class="arg-item"
|
||||
>
|
||||
<t-input
|
||||
v-model="formData.stdio_config.args[index]"
|
||||
:placeholder="`参数 ${index + 1}`"
|
||||
class="arg-input"
|
||||
/>
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="danger"
|
||||
@click="removeArg(index)"
|
||||
:disabled="formData.stdio_config.args.length === 1"
|
||||
>
|
||||
<template #icon><t-icon name="delete" /></template>
|
||||
</t-button>
|
||||
</div>
|
||||
<t-button
|
||||
variant="outline"
|
||||
size="small"
|
||||
@click="addArg"
|
||||
class="add-arg-btn"
|
||||
>
|
||||
<template #icon><t-icon name="add" /></template>
|
||||
添加参数
|
||||
</t-button>
|
||||
</div>
|
||||
</t-form-item>
|
||||
|
||||
<t-form-item label="环境变量">
|
||||
<div class="env-vars-container">
|
||||
<div
|
||||
v-for="(value, key, index) in formData.env_vars"
|
||||
:key="index"
|
||||
class="env-var-item"
|
||||
>
|
||||
<t-input
|
||||
v-model="envVarKeys[index]"
|
||||
placeholder="变量名"
|
||||
class="env-key-input"
|
||||
@blur="updateEnvVarKey(index, envVarKeys[index])"
|
||||
/>
|
||||
<t-input
|
||||
v-model="formData.env_vars[key]"
|
||||
placeholder="变量值"
|
||||
type="password"
|
||||
class="env-value-input"
|
||||
/>
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="danger"
|
||||
@click="removeEnvVar(key)"
|
||||
>
|
||||
<template #icon><t-icon name="delete" /></template>
|
||||
</t-button>
|
||||
</div>
|
||||
<t-button
|
||||
variant="outline"
|
||||
size="small"
|
||||
@click="addEnvVar"
|
||||
class="add-env-var-btn"
|
||||
>
|
||||
<template #icon><t-icon name="add" /></template>
|
||||
添加环境变量
|
||||
</t-button>
|
||||
</div>
|
||||
</t-form-item>
|
||||
</template>
|
||||
|
||||
<t-form-item label="启用服务" name="enabled">
|
||||
<t-switch v-model="formData.enabled" />
|
||||
</t-form-item>
|
||||
@@ -122,8 +210,13 @@ const formData = ref({
|
||||
name: '',
|
||||
description: '',
|
||||
enabled: true,
|
||||
transport_type: 'sse' as 'sse' | 'http-streamable',
|
||||
transport_type: 'sse' as 'sse' | 'http-streamable' | 'stdio',
|
||||
url: '',
|
||||
stdio_config: {
|
||||
command: 'uvx' as 'uvx' | 'npx',
|
||||
args: ['']
|
||||
},
|
||||
env_vars: {} as Record<string, string>,
|
||||
auth_config: {
|
||||
api_key: '',
|
||||
token: ''
|
||||
@@ -135,12 +228,54 @@ const formData = ref({
|
||||
}
|
||||
})
|
||||
|
||||
// Track env var keys separately for easier editing
|
||||
const envVarKeys = ref<string[]>([])
|
||||
|
||||
const rules: Record<string, FormRule[]> = {
|
||||
name: [{ required: true, message: '请输入服务名称', type: 'error' }],
|
||||
transport_type: [{ required: true, message: '请选择传输类型', type: 'error' }],
|
||||
url: [
|
||||
{ required: true, message: '请输入服务 URL', type: 'error' },
|
||||
{ url: true, message: '请输入有效的 URL', type: 'error' }
|
||||
{
|
||||
validator: (val: string) => {
|
||||
if (formData.value.transport_type !== 'stdio') {
|
||||
if (!val || val.trim() === '') {
|
||||
return { result: false, message: '请输入服务 URL', type: 'error' }
|
||||
}
|
||||
// Basic URL validation
|
||||
try {
|
||||
new URL(val)
|
||||
return { result: true }
|
||||
} catch {
|
||||
return { result: false, message: '请输入有效的 URL', type: 'error' }
|
||||
}
|
||||
}
|
||||
return { result: true }
|
||||
}
|
||||
}
|
||||
],
|
||||
'stdio_config.command': [
|
||||
{
|
||||
validator: (val: string) => {
|
||||
if (formData.value.transport_type === 'stdio') {
|
||||
if (!val || (val !== 'uvx' && val !== 'npx')) {
|
||||
return { result: false, message: '请选择命令 (uvx 或 npx)', type: 'error' }
|
||||
}
|
||||
}
|
||||
return { result: true }
|
||||
}
|
||||
}
|
||||
],
|
||||
'stdio_config.args': [
|
||||
{
|
||||
validator: (val: string[]) => {
|
||||
if (formData.value.transport_type === 'stdio') {
|
||||
if (!val || val.length === 0 || val.every(arg => !arg || arg.trim() === '')) {
|
||||
return { result: false, message: '请至少输入一个参数', type: 'error' }
|
||||
}
|
||||
}
|
||||
return { result: true }
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -157,6 +292,11 @@ const resetForm = () => {
|
||||
enabled: true,
|
||||
transport_type: 'sse',
|
||||
url: '',
|
||||
stdio_config: {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
},
|
||||
env_vars: {},
|
||||
auth_config: {
|
||||
api_key: '',
|
||||
token: ''
|
||||
@@ -167,9 +307,74 @@ const resetForm = () => {
|
||||
retry_delay: 1
|
||||
}
|
||||
}
|
||||
envVarKeys.value = []
|
||||
formRef.value?.clearValidate()
|
||||
}
|
||||
|
||||
// Watch transport_type to reset related fields
|
||||
watch(
|
||||
() => formData.value.transport_type,
|
||||
(newType) => {
|
||||
if (newType === 'stdio') {
|
||||
formData.value.url = ''
|
||||
if (!formData.value.stdio_config || formData.value.stdio_config.args.length === 0) {
|
||||
formData.value.stdio_config = {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
}
|
||||
}
|
||||
} else {
|
||||
formData.value.stdio_config = {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
}
|
||||
formData.value.env_vars = {}
|
||||
envVarKeys.value = []
|
||||
}
|
||||
formRef.value?.clearValidate()
|
||||
}
|
||||
)
|
||||
|
||||
// Args management
|
||||
const addArg = () => {
|
||||
formData.value.stdio_config.args.push('')
|
||||
}
|
||||
|
||||
const removeArg = (index: number) => {
|
||||
if (formData.value.stdio_config.args.length > 1) {
|
||||
formData.value.stdio_config.args.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// Env vars management
|
||||
const addEnvVar = () => {
|
||||
const key = `VAR_${Date.now()}`
|
||||
formData.value.env_vars[key] = ''
|
||||
envVarKeys.value.push(key)
|
||||
}
|
||||
|
||||
const removeEnvVar = (key: string) => {
|
||||
delete formData.value.env_vars[key]
|
||||
const index = envVarKeys.value.indexOf(key)
|
||||
if (index > -1) {
|
||||
envVarKeys.value.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
const updateEnvVarKey = (index: number, newKey: string) => {
|
||||
const oldKey = envVarKeys.value[index]
|
||||
if (oldKey && oldKey !== newKey && formData.value.env_vars[oldKey] !== undefined) {
|
||||
const value = formData.value.env_vars[oldKey]
|
||||
delete formData.value.env_vars[oldKey]
|
||||
if (newKey && newKey.trim() !== '') {
|
||||
formData.value.env_vars[newKey] = value
|
||||
envVarKeys.value[index] = newKey
|
||||
} else {
|
||||
envVarKeys.value.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Watch service prop to initialize form
|
||||
watch(
|
||||
() => props.service,
|
||||
@@ -181,6 +386,11 @@ watch(
|
||||
enabled: service.enabled ?? true,
|
||||
transport_type: service.transport_type || 'sse',
|
||||
url: service.url || '',
|
||||
stdio_config: service.stdio_config || {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
},
|
||||
env_vars: service.env_vars || {},
|
||||
auth_config: {
|
||||
api_key: service.auth_config?.api_key || '',
|
||||
token: service.auth_config?.token || ''
|
||||
@@ -191,6 +401,8 @@ watch(
|
||||
retry_delay: service.advanced_config?.retry_delay || 1
|
||||
}
|
||||
}
|
||||
// Initialize env var keys
|
||||
envVarKeys.value = Object.keys(formData.value.env_vars)
|
||||
} else {
|
||||
resetForm()
|
||||
}
|
||||
@@ -210,7 +422,6 @@ const handleSubmit = async () => {
|
||||
description: formData.value.description,
|
||||
enabled: formData.value.enabled,
|
||||
transport_type: formData.value.transport_type,
|
||||
url: formData.value.url,
|
||||
auth_config: {
|
||||
api_key: formData.value.auth_config.api_key || undefined,
|
||||
token: formData.value.auth_config.token || undefined
|
||||
@@ -218,6 +429,26 @@ const handleSubmit = async () => {
|
||||
advanced_config: formData.value.advanced_config
|
||||
}
|
||||
|
||||
// Add URL or stdio_config based on transport type
|
||||
if (formData.value.transport_type === 'stdio') {
|
||||
// Filter out empty args
|
||||
const args = formData.value.stdio_config.args.filter(arg => arg && arg.trim() !== '')
|
||||
data.stdio_config = {
|
||||
command: formData.value.stdio_config.command,
|
||||
args: args.length > 0 ? args : undefined
|
||||
}
|
||||
// Filter out empty env vars
|
||||
const envVars: Record<string, string> = {}
|
||||
for (const [key, value] of Object.entries(formData.value.env_vars)) {
|
||||
if (key && key.trim() !== '' && value && value.trim() !== '') {
|
||||
envVars[key] = value
|
||||
}
|
||||
}
|
||||
data.env_vars = Object.keys(envVars).length > 0 ? envVars : undefined
|
||||
} else {
|
||||
data.url = formData.value.url || undefined
|
||||
}
|
||||
|
||||
if (props.mode === 'add') {
|
||||
await createMCPService(data)
|
||||
MessagePlugin.success('MCP 服务已创建')
|
||||
@@ -243,3 +474,49 @@ const handleClose = () => {
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.args-input-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
|
||||
.arg-item {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
|
||||
.arg-input {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.add-arg-btn {
|
||||
align-self: flex-start;
|
||||
}
|
||||
}
|
||||
|
||||
.env-vars-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
|
||||
.env-var-item {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
|
||||
.env-key-input {
|
||||
width: 150px;
|
||||
}
|
||||
|
||||
.env-value-input {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.add-env-var-btn {
|
||||
align-self: flex-start;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user