Agnes 图像 / 视频生成接进 DSH

Posted by 晴天 on 2026年10月11日

Agnes 图像 / 视频生成接进 DSH

DSH 的模型配置只走 /v1/chat/completions,所以 agnes-image-2.5-flash 这类生成模型 不能加进 settings.yaml 的 models 列表(加了也是选中即报 400)。

正确做法:把它做成一个 MCP 工具。


1. 保存代码

新建文件夹 D:\bt\x86\ai-agent\agnes-media-mcp\,把下面代码存成 server.mjs(UTF-8)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/env node
/**
 * Agnes AI media MCP server — image and video generation over the Model Context
 * Protocol, stdio transport.
 *
 * Zero dependencies on purpose: the MCP stdio wire format (newline-delimited
 * JSON-RPC 2.0) is implemented directly, so DSH can spawn this file with a bare
 * `node` and there is no `npm install` step to break.
 *
 * stdout carries ONLY JSON-RPC frames. Every diagnostic goes to stderr.
 *
 * Environment:
 *   AGNES_AI_API_KEY       API key. Falls back to ~/.dsh/.credentials.yaml.
 *   AGNES_AI_BASE_URL      Default https://api.agnes-ai.cn/v1
 *   AGNES_MEDIA_OUTPUT_DIR Default <this file's directory>/output
 */

import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { homedir } from 'node:os'
import { dirname, extname, isAbsolute, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

const SERVER_NAME = 'agnes-media'
const SERVER_VERSION = '1.0.0'
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))

const DEFAULT_BASE_URL = 'https://api.agnes-ai.cn/v1'
const DEFAULT_IMAGE_MODEL = 'agnes-image-2.5-flash'
const DEFAULT_VIDEO_MODEL = 'agnes-video-2.5-flash'

/** Protocol revisions this server knows how to speak, newest first. */
const SUPPORTED_PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05']

/** Largest generated image we inline as an MCP image block (raw bytes). */
const MAX_INLINE_IMAGE_BYTES = 12 * 1024 * 1024

// ---------------------------------------------------------------------------
// logging (stderr only — stdout belongs to the protocol)
// ---------------------------------------------------------------------------

function log(message) {
  process.stderr.write(`[${SERVER_NAME}] ${message}\n`)
}

function sleep(ms) {
  return new Promise((done) => setTimeout(done, ms))
}

// ---------------------------------------------------------------------------
// configuration
// ---------------------------------------------------------------------------

function apiBase() {
  return (process.env.AGNES_AI_BASE_URL || DEFAULT_BASE_URL).replace(/\/+$/, '')
}

function apiOrigin() {
  return new URL(apiBase()).origin
}

function outputDir() {
  return process.env.AGNES_MEDIA_OUTPUT_DIR || join(SCRIPT_DIR, 'output')
}

async function resolveApiKey() {
  const fromEnv = (process.env.AGNES_AI_API_KEY || '').trim()
  if (fromEnv) return fromEnv
  // The DSH-subprocess seam scrubs credential-shaped names from the parent
  // environment, so fall back to the harness credential store on disk.
  const credPath = join(process.env.DSH_HOME || join(homedir(), '.dsh'), '.credentials.yaml')
  try {
    const text = await readFile(credPath, 'utf8')
    const match = text.match(/^\s*AGNES_AI_API_KEY\s*:\s*(.+?)\s*$/m)
    if (match) return match[1].replace(/^['"]|['"]$/g, '')
  } catch {
    /* fall through to the error below */
  }
  throw new Error(
    `no Agnes API key: set AGNES_AI_API_KEY in this MCP server's env, or add AGNES_AI_API_KEY to ${credPath}`,
  )
}

// ---------------------------------------------------------------------------
// HTTP
// ---------------------------------------------------------------------------

async function requestJson(url, { method = 'GET', body, timeoutMs = 120000 } = {}) {
  const key = await resolveApiKey()
  const headers = { Authorization: `Bearer ${key}` }
  if (body !== undefined) headers['Content-Type'] = 'application/json'
  const response = await fetch(url, {
    method,
    headers,
    body: body === undefined ? undefined : JSON.stringify(body),
    signal: AbortSignal.timeout(timeoutMs),
  })
  const text = await response.text()
  let parsed
  try {
    parsed = text ? JSON.parse(text) : undefined
  } catch {
    parsed = undefined
  }
  if (!response.ok) {
    const detail =
      parsed?.error?.message ||
      parsed?.message ||
      parsed?.detail ||
      (typeof parsed?.detail === 'string' ? parsed.detail : undefined) ||
      text.slice(0, 400)
    const error = new Error(`Agnes API HTTP ${response.status}: ${detail || response.statusText}`)
    error.status = response.status
    error.payload = parsed
    throw error
  }
  return parsed ?? {}
}

async function downloadBytes(url, timeoutMs = 300000) {
  const response = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) })
  if (!response.ok) throw new Error(`could not download ${url}: HTTP ${response.status}`)
  return Buffer.from(await response.arrayBuffer())
}

// ---------------------------------------------------------------------------
// files
// ---------------------------------------------------------------------------

function timestamp() {
  return new Date().toISOString().replace(/[-:T]/g, '').slice(0, 14)
}

function uniqueName(prefix, extension) {
  return `${prefix}-${timestamp()}-${Math.random().toString(36).slice(2, 8)}${extension}`
}

function resolveOutputDir(saveDir) {
  if (typeof saveDir === 'string' && saveDir.trim()) {
    const dir = saveDir.trim()
    return isAbsolute(dir) ? dir : resolve(process.cwd(), dir)
  }
  return outputDir()
}

async function writeOutput(dir, filename, buffer) {
  await mkdir(dir, { recursive: true })
  const target = join(dir, filename)
  await writeFile(target, buffer)
  return target
}

const IMAGE_MIME_BY_EXTENSION = {
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.jpeg': 'image/jpeg',
  '.webp': 'image/webp',
  '.gif': 'image/gif',
}

const IMAGE_EXTENSION_BY_MIME = {
  'image/png': '.png',
  'image/jpeg': '.jpg',
  'image/webp': '.webp',
  'image/gif': '.gif',
}

/** DSH admits PNG/JPEG/WebP/GIF image blocks; anything else is projected to text. */
function detectImageMime(buffer) {
  if (buffer.length >= 8 && buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e) return 'image/png'
  if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return 'image/jpeg'
  if (
    buffer.length >= 12 &&
    buffer.toString('ascii', 0, 4) === 'RIFF' &&
    buffer.toString('ascii', 8, 12) === 'WEBP'
  ) {
    return 'image/webp'
  }
  if (buffer.length >= 6 && buffer.toString('ascii', 0, 3) === 'GIF') return 'image/gif'
  return null
}

/** Turn a URL, data URI, or local path into something Agnes accepts as input. */
async function toImageReference(value) {
  const text = String(value ?? '').trim()
  if (!text) throw new Error('empty image reference')
  if (/^https?:\/\//i.test(text) || text.startsWith('data:')) return text
  const path = isAbsolute(text) ? text : resolve(process.cwd(), text)
  const buffer = await readFile(path)
  const mime = IMAGE_MIME_BY_EXTENSION[extname(path).toLowerCase()] || 'image/png'
  return `data:${mime};base64,${buffer.toString('base64')}`
}

/**
 * Video reference media must be publicly reachable by Agnes, so a local path
 * cannot be forwarded and is rejected with the documented workaround.
 */
function requirePublicUrl(value, field) {
  const text = String(value ?? '').trim()
  if (/^https?:\/\//i.test(text)) return text
  throw new Error(
    `${field} must be a public HTTPS URL that Agnes can fetch. ` +
      `Local files cannot be used here — call generate_image first and pass the URL it returns.`,
  )
}

// ---------------------------------------------------------------------------
// tool implementations
// ---------------------------------------------------------------------------

function requireString(args, key) {
  const value = args[key]
  if (typeof value !== 'string' || !value.trim()) throw new Error(`"${key}" is required and must be a non-empty string`)
  return value.trim()
}

function optionalStringList(value, field) {
  if (value === undefined || value === null) return []
  if (!Array.isArray(value)) throw new Error(`"${field}" must be an array of strings`)
  return value.map((entry) => String(entry)).filter((entry) => entry.trim())
}

async function generateImage(args) {
  const prompt = requireString(args, 'prompt')
  const size = typeof args.size === 'string' && args.size.trim() ? args.size.trim() : '2K'
  const ratio = typeof args.ratio === 'string' && args.ratio.trim() ? args.ratio.trim() : '1:1'
  const model = typeof args.model === 'string' && args.model.trim() ? args.model.trim() : DEFAULT_IMAGE_MODEL
  const inputs = optionalStringList(args.images, 'images')

  const extraBody = { response_format: inputs.length ? 'b64_json' : 'url' }
  if (inputs.length) extraBody.image = await Promise.all(inputs.map(toImageReference))

  const payload = { model, prompt, size, ratio, extra_body: extraBody }
  log(`images/generations model=${model} size=${size} ratio=${ratio} refs=${inputs.length}`)
  const response = await requestJson(`${apiBase()}/images/generations`, {
    method: 'POST',
    body: payload,
    timeoutMs: 360000,
  })

  const first = Array.isArray(response.data) ? response.data[0] : undefined
  if (!first) throw new Error('Agnes returned no image data')

  let buffer
  let sourceUrl
  if (typeof first.b64_json === 'string' && first.b64_json) {
    buffer = Buffer.from(first.b64_json, 'base64')
  } else if (typeof first.url === 'string' && first.url) {
    sourceUrl = first.url
    buffer = await downloadBytes(first.url)
  } else {
    throw new Error('Agnes returned neither url nor b64_json')
  }

  const mime = detectImageMime(buffer) || 'image/png'
  const extension = IMAGE_EXTENSION_BY_MIME[mime] || '.png'
  const target = await writeOutput(resolveOutputDir(args.save_dir), uniqueName('agnes-image', extension), buffer)

  const lines = [
    `Generated image (${model}, ${size} ${ratio}).`,
    sourceUrl ? `Source URL: ${sourceUrl}` : 'Source: base64 response',
    `Saved to: ${target}`,
    `Bytes: ${buffer.length}`,
  ]
  const content = [{ type: 'text', text: lines.join('\n') }]

  if (buffer.length <= MAX_INLINE_IMAGE_BYTES) {
    content.push({ type: 'image', data: buffer.toString('base64'), mimeType: mime })
  } else {
    content.push({
      type: 'text',
      text: `(${buffer.length} bytes exceeds the inline limit; open the saved file instead)`,
    })
  }
  return content
}

/**
 * Create a video task, retrying while the service reports a full queue.
 * `video_queue_full` is a 503 the docs ask callers to retry, not a bad request.
 */
async function createVideoTask(payload, attempts = 6) {
  let lastError
  for (let attempt = 0; attempt < attempts; attempt += 1) {
    try {
      return await requestJson(`${apiBase()}/videos`, { method: 'POST', body: payload, timeoutMs: 180000 })
    } catch (error) {
      lastError = error
      const queueFull =
        error.status === 503 || /queue_full|队列已满|queue is full/i.test(String(error.message || ''))
      if (!queueFull || attempt === attempts - 1) throw error
      const waitMs = Math.min(30000, 3000 * 2 ** attempt)
      log(`video queue full, retrying in ${waitMs}ms (attempt ${attempt + 2}/${attempts})`)
      await sleep(waitMs)
    }
  }
  throw lastError
}

function videoIdOf(task) {
  const id = task?.video_id || task?.id || task?.task_id
  if (typeof id !== 'string' || !id) throw new Error('Agnes returned no video_id')
  return id
}

/** Poll GET /agnesapi until the task settles. Lives at the origin, not under /v1. */
async function pollVideoTask(videoId, model, timeoutMs, intervalMs) {
  const url =
    `${apiOrigin()}/agnesapi?video_id=${encodeURIComponent(videoId)}` +
    `&model_name=${encodeURIComponent(model)}`
  const deadline = Date.now() + timeoutMs
  let last
  while (Date.now() < deadline) {
    await sleep(intervalMs)
    last = await requestJson(url, { timeoutMs: 60000 })
    const status = String(last?.status || '')
    if (status === 'completed') return last
    if (status === 'failed') {
      const reason = last?.error?.message || last?.error || 'no reason reported'
      throw new Error(`video task ${videoId} failed: ${typeof reason === 'string' ? reason : JSON.stringify(reason)}`)
    }
  }
  const error = new Error(
    `video task ${videoId} did not finish within ${Math.round(timeoutMs / 1000)}s ` +
      `(last status: ${last?.status ?? 'unknown'}). Call video_status with this id to keep checking.`,
  )
  error.videoId = videoId
  throw error
}

async function generateVideo(args) {
  const prompt = requireString(args, 'prompt')
  const model = typeof args.model === 'string' && args.model.trim() ? args.model.trim() : DEFAULT_VIDEO_MODEL
  const mode = typeof args.mode === 'string' && args.mode.trim() ? args.mode.trim() : 'text'
  if (!['text', 'keyframe', 'reference'].includes(mode)) {
    throw new Error(`"mode" must be one of text, keyframe, reference (got "${mode}")`)
  }
  const seconds = String(args.seconds ?? '5')

  const payload = {
    model,
    prompt,
    mode,
    seconds,
    size: '720P', // Agnes Video 2.5 Flash accepts only "720P"
    aspect_ratio: typeof args.aspect_ratio === 'string' && args.aspect_ratio.trim() ? args.aspect_ratio.trim() : '16:9',
  }

  if (mode === 'keyframe') {
    const first = args.first_frame ? requirePublicUrl(args.first_frame, 'first_frame') : undefined
    const last = args.last_frame ? requirePublicUrl(args.last_frame, 'last_frame') : undefined
    if (!first && !last) throw new Error('keyframe mode needs first_frame and/or last_frame')
    if (first) payload.first_frame = first
    if (last) payload.last_frame = last
  }

  if (mode === 'reference') {
    const images = optionalStringList(args.images, 'images').map((entry) => requirePublicUrl(entry, 'images'))
    const audios = optionalStringList(args.audios, 'audios').map((entry) => requirePublicUrl(entry, 'audios'))
    if (images.length > 5) throw new Error('agnes-video-2.5-flash accepts at most 5 reference images')
    if (audios.length > 3) throw new Error('agnes-video-2.5-flash accepts at most 3 reference audios')
    if (!images.length && !audios.length) throw new Error('reference mode needs at least one image or audio URL')
    if (images.length) payload.images = images
    if (audios.length) payload.audios = audios
  }

  log(`videos model=${model} mode=${mode} seconds=${seconds}`)
  const created = await createVideoTask(payload)
  const videoId = videoIdOf(created)

  const timeoutMs = Math.max(30, Number(args.max_wait_seconds) || 900) * 1000
  const intervalMs = Math.min(10000, Math.max(1000, (Number(args.poll_interval_seconds) || 2) * 1000))
  const finished = await pollVideoTask(videoId, model, timeoutMs, intervalMs)

  const url = typeof finished.url === 'string' ? finished.url : undefined
  const lines = [
    `Generated video (${model}, mode=${mode}, ${finished.seconds ?? seconds}s, ${finished.size ?? '720P'}).`,
    `Task video_id: ${videoId}`,
  ]

  if (url) {
    lines.push(`Video URL: ${url}`)
    try {
      const buffer = await downloadBytes(url)
      const target = await writeOutput(resolveOutputDir(args.save_dir), uniqueName('agnes-video', '.mp4'), buffer)
      lines.push(`Saved to: ${target}`, `Bytes: ${buffer.length}`)
    } catch (error) {
      lines.push(`Download failed (the URL above is still valid): ${error.message}`)
    }
  } else {
    lines.push('Task completed without a url field.')
  }

  return [{ type: 'text', text: lines.join('\n') }]
}

async function videoStatus(args) {
  const videoId = requireString(args, 'video_id')
  const model = typeof args.model === 'string' && args.model.trim() ? args.model.trim() : DEFAULT_VIDEO_MODEL
  const url =
    `${apiOrigin()}/agnesapi?video_id=${encodeURIComponent(videoId)}` +
    `&model_name=${encodeURIComponent(model)}`
  const task = await requestJson(url, { timeoutMs: 60000 })

  const lines = [
    `video_id: ${videoId}`,
    `status: ${task?.status ?? 'unknown'}`,
    `progress: ${task?.progress ?? 'unknown'}`,
  ]
  if (typeof task?.url === 'string' && task.url) lines.push(`Video URL: ${task.url}`)
  if (task?.error) lines.push(`error: ${typeof task.error === 'string' ? task.error : JSON.stringify(task.error)}`)

  const content = [{ type: 'text', text: lines.join('\n') }]
  if (String(task?.status) === 'completed' && typeof task.url === 'string' && task.url) {
    try {
      const buffer = await downloadBytes(task.url)
      const target = await writeOutput(resolveOutputDir(args.save_dir), uniqueName('agnes-video', '.mp4'), buffer)
      content.push({ type: 'text', text: `Saved to: ${target}` })
    } catch {
      /* the URL line already carries the result */
    }
  }
  return content
}

// ---------------------------------------------------------------------------
// tool declarations
//
// The inputSchema subset DSH enforces is type/oneOf/properties/required/
// additionalProperties/items/enum/const plus description/title/default/examples.
// Anything else (format, minimum, pattern, anyOf, ...) fails tool registration.
// ---------------------------------------------------------------------------

const TOOLS = [
  {
    name: 'generate_image',
    description:
      'Generate or edit an image with Agnes AI (POST /v1/images/generations). ' +
      'Text-to-image by default; pass `images` for image-to-image or multi-image composition. ' +
      'The generated picture is saved to disk and also returned inline so it renders in the conversation.',
    inputSchema: {
      type: 'object',
      properties: {
        prompt: {
          type: 'string',
          description:
            'What to draw or how to edit. Structure: subject + scene + style + lighting + composition + quality.',
        },
        size: {
          type: 'string',
          enum: ['1K', '2K', '3K', '4K'],
          default: '2K',
          description: 'Output resolution tier. Combine with `ratio`.',
        },
        ratio: {
          type: 'string',
          enum: ['1:1', '3:4', '4:3', '16:9', '9:16', '2:3', '3:2', '21:9'],
          default: '1:1',
          description: 'Aspect ratio used together with the `size` tier.',
        },
        images: {
          type: 'array',
          items: { type: 'string' },
          description:
            'Input references for image-to-image or multi-image composition. ' +
            'Each entry is a public HTTPS image URL, a data URI, or a local file path.',
        },
        model: {
          type: 'string',
          default: 'agnes-image-2.5-flash',
          description: 'Image model id (agnes-image-2.5-flash or agnes-image-2.1-flash).',
        },
        save_dir: {
          type: 'string',
          description: 'Directory for the saved file. Defaults to AGNES_MEDIA_OUTPUT_DIR.',
        },
      },
      required: ['prompt'],
    },
    run: generateImage,
  },
  {
    name: 'generate_video',
    description:
      'Generate a video with Agnes AI (POST /v1/videos, asynchronous) and wait for it. ' +
      'Creates the task, polls until it completes, then downloads the MP4. ' +
      'Reference media must be publicly reachable URLs. Video generation can take minutes.',
    inputSchema: {
      type: 'object',
      properties: {
        prompt: {
          type: 'string',
          description: 'Video description. In reference mode use <Picture N> / <Audio N> to cite the media.',
        },
        mode: {
          type: 'string',
          enum: ['text', 'keyframe', 'reference'],
          default: 'text',
          description:
            'text = prompt only; keyframe = drive from first/last frame; reference = condition on image/audio URLs.',
        },
        seconds: {
          type: 'string',
          enum: ['4', '5', '6', '7', '8', '9', '10', '11', '12'],
          default: '5',
          description: 'Clip length in seconds.',
        },
        aspect_ratio: {
          type: 'string',
          enum: ['21:9', '16:9', '4:3', '1:1', '3:4', '9:16'],
          default: '16:9',
          description: 'Output frame. Resolution is fixed at 720P for the flash model.',
        },
        first_frame: {
          type: 'string',
          description: 'keyframe mode: public HTTPS URL of the first frame.',
        },
        last_frame: {
          type: 'string',
          description: 'keyframe mode: public HTTPS URL of the last frame.',
        },
        images: {
          type: 'array',
          items: { type: 'string' },
          description: 'reference mode: up to 5 public HTTPS image URLs.',
        },
        audios: {
          type: 'array',
          items: { type: 'string' },
          description: 'reference mode: up to 3 public HTTPS audio URLs.',
        },
        model: {
          type: 'string',
          default: 'agnes-video-2.5-flash',
          description: 'Video model id (agnes-video-2.5-flash or agnes-video-2.5).',
        },
        max_wait_seconds: {
          type: 'integer',
          default: 900,
          description: 'Give up waiting after this long and report the task id instead.',
        },
        poll_interval_seconds: {
          type: 'integer',
          default: 2,
          description: 'Seconds between status polls.',
        },
        save_dir: {
          type: 'string',
          description: 'Directory for the saved MP4. Defaults to AGNES_MEDIA_OUTPUT_DIR.',
        },
      },
      required: ['prompt'],
    },
    run: generateVideo,
  },
  {
    name: 'video_status',
    description:
      'Check an Agnes video task by its video_id and download the MP4 once it is complete. ' +
      'Use this after generate_video timed out or returned a task id.',
    inputSchema: {
      type: 'object',
      properties: {
        video_id: { type: 'string', description: 'The video_id returned when the task was created.' },
        model: {
          type: 'string',
          default: 'agnes-video-2.5-flash',
          description: 'Must match the model that created the task.',
        },
        save_dir: {
          type: 'string',
          description: 'Directory for the saved MP4. Defaults to AGNES_MEDIA_OUTPUT_DIR.',
        },
      },
      required: ['video_id'],
    },
    run: videoStatus,
  },
]

// ---------------------------------------------------------------------------
// MCP stdio transport (newline-delimited JSON-RPC 2.0)
// ---------------------------------------------------------------------------

function send(message) {
  process.stdout.write(`${JSON.stringify(message)}\n`)
}

function sendResult(id, result) {
  send({ jsonrpc: '2.0', id, result })
}

function sendError(id, code, message) {
  send({ jsonrpc: '2.0', id, error: { code, message } })
}

function initializeResult(params) {
  const requested = params && typeof params.protocolVersion === 'string' ? params.protocolVersion : undefined
  const protocolVersion =
    requested && SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : SUPPORTED_PROTOCOL_VERSIONS[0]
  return {
    protocolVersion,
    capabilities: { tools: { listChanged: false } },
    serverInfo: { name: SERVER_NAME, version: SERVER_VERSION },
  }
}

async function callTool(params) {
  const name = params && typeof params.name === 'string' ? params.name : ''
  const args = params && typeof params.arguments === 'object' && params.arguments !== null ? params.arguments : {}
  const tool = TOOLS.find((candidate) => candidate.name === name)
  if (!tool) return { content: [{ type: 'text', text: `Unknown tool: ${name}` }], isError: true }
  try {
    return { content: await tool.run(args) }
  } catch (error) {
    const message = error && error.message ? error.message : String(error)
    log(`${name} failed: ${message}`)
    return { content: [{ type: 'text', text: `${name} failed: ${message}` }], isError: true }
  }
}

async function handleMessage(message) {
  if (!message || typeof message !== 'object' || Array.isArray(message)) return
  const { id, method, params } = message
  // A message without a method is a response to a server-initiated request; this
  // server issues none. A message without an id is a notification: never answer.
  if (typeof method !== 'string' || id === undefined || id === null) return

  switch (method) {
    case 'initialize':
      sendResult(id, initializeResult(params))
      return
    case 'ping':
      sendResult(id, {})
      return
    case 'tools/list':
      sendResult(id, {
        tools: TOOLS.map(({ name, description, inputSchema }) => ({ name, description, inputSchema })),
      })
      return
    case 'tools/call':
      sendResult(id, await callTool(params))
      return
    case 'resources/list':
      sendResult(id, { resources: [] })
      return
    case 'resources/templates/list':
      sendResult(id, { resourceTemplates: [] })
      return
    case 'prompts/list':
      sendResult(id, { prompts: [] })
      return
    default:
      sendError(id, -32601, `Method not found: ${method}`)
  }
}

let pending = ''
/** In-flight request handlers, so a closing stdin can wait for a slow tool call. */
const inFlight = new Set()

function track(promise) {
  inFlight.add(promise)
  void promise.finally(() => inFlight.delete(promise))
  return promise
}

process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk) => {
  pending += chunk
  let newline = pending.indexOf('\n')
  while (newline !== -1) {
    const line = pending.slice(0, newline).trim()
    pending = pending.slice(newline + 1)
    if (line) {
      track(
        handleMessage(safeParse(line)).catch((error) => {
          log(`message handling failed: ${error && error.message ? error.message : String(error)}`)
        }),
      )
    }
    newline = pending.indexOf('\n')
  }
})
process.stdin.on('end', () => {
  void (async () => {
    if (inFlight.size > 0) {
      log(`stdin closed; draining ${inFlight.size} in-flight request(s)`)
      await Promise.allSettled([...inFlight])
    }
    process.exit(0)
  })()
})
process.stdin.on('error', (error) => {
  log(`stdin error: ${error.message}`)
  process.exit(1)
})

function safeParse(line) {
  try {
    return JSON.parse(line)
  } catch {
    log('ignoring a non-JSON line on stdin')
    return undefined
  }
}

log(`ready — base=${apiBase()} output=${outputDir()} tools=${TOOLS.map((tool) => tool.name).join(',')}`)

2. 在 Harness 里配置 MCP

打开 DSH → 设置 → MCP 管理 → 添加服务器,按下表填写:

字段 值
serverName agnes-media
传输方式 stdio
command C:\Program Files\nodejs\node.exe
args D:\bt\x86\ai-agent\agnes-media-mcp\server.mjs
env AGNES_AI_API_KEY=sk-你的Key
AGNES_MEDIA_OUTPUT_DIR=D:\bt\x86\ai-agent\agnes-media-out
级别 项目级

command 要用 node.exe 的绝对路径(用 (Get-Command node).Source 查)。 Key 必须写在 env 里——DSH 会擦除父进程里形似凭证的环境变量。

等价的 cordis.patch.yml 写法(路径 C:\Users\Administrator\.dsh\profiles\web\cordis.patch.yml):

1
2
3
4
5
6
7
8
9
10
11
12
- insert:
    - id: mcp-agnes-media
      name: '@deepseek-ai/dsh-mcp-client'
      config:
        serverName: "agnes-media"
        transport: "stdio"
        command: "C:\\Program Files\\nodejs\\node.exe"
        args:
          - "D:\\bt\\x86\\ai-agent\\agnes-media-mcp\\server.mjs"
        env:
          "AGNES_AI_API_KEY": "sk-你的Key"
          "AGNES_MEDIA_OUTPUT_DIR": "D:\\bt\\x86\\ai-agent\\agnes-media-out"

注意必须用 - insert: 包起来。DSH 里 - id: 开头的行只能覆盖已有条目,装不上新插件。

验证:回到 设置 → MCP 管理,该卡片应显示 enabled、tools:3。


3. 使用

注册后 DSH 里的工具名是 mcp__agnes-media__<工具名>:

工具 作用
mcp__agnes-media__generate_image 文生图 / 图生图 / 多图合成,图片直接显示在对话里
mcp__agnes-media__generate_video 文生视频 / 首尾帧 / 素材参考,完成后下载 MP4
mcp__agnes-media__video_status 用 video_id 复查任务

直接对话即可:

1
生成一张 16:9 的 2K 电影感产品图:一台显示器摆在干净的白色桌面上,柔和侧光,高细节
1
生成一段 5 秒 16:9 的视频:雨后未来城市街道,霓虹倒映在湿滑路面,一辆银色跑车缓缓驶过

generate_image 的 images 参数支持本地文件路径(会自动转 base64)。 generate_video 的参考素材必须是公网 URL——先调 generate_image 拿到它返回的 URL 再喂进去。


4. 常见报错

报错 原因 / 解决
400 ... 是 image 模型,请使用 /v1/images/generations 生成模型被配成了对话模型,从 settings.yaml 的 models 里删掉
503 video_queue_full 服务端排队,不是配置错。脚本已内置 6 次退避重试,仍失败就稍后再试
429 rate_limit_exceeded 账号免费额度速率限制,需冷却或升级 Token Plan
tools:0 / loader:off command 或 args 路径写错,检查绝对路径
图片只有 URL 不显示 DSH 只接受 PNG/JPEG/WebP/GIF,且超过 12 MiB 会降级为文本+路径

5. 环境变量

变量 默认 说明
AGNES_AI_API_KEY 回退读 ~/.dsh/.credentials.yaml API Key
AGNES_AI_BASE_URL https://api.agnes-ai.cn/v1 API 根地址
AGNES_MEDIA_OUTPUT_DIR <脚本目录>/output 图片 / 视频保存目录