Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dev": "tsc -w",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "prettier --check \"src/**/*.ts\"",
"test": "node --test tests/rest-async-video.test.mjs",
"prepare": "pnpm run build",
"pretest": "pnpm run build",
"inspector": "npx @modelcontextprotocol/inspector build/index.js"
Expand Down
7 changes: 5 additions & 2 deletions src/services/media-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ export class MediaService extends BaseService {
public async generateVideo(params: any): Promise<any> {
this.checkInitialized();
try {
// REST tools expose the option as async_mode, while VideoAPI uses asyncMode.
const asyncMode = params.asyncMode ?? params.async_mode;

// Auto-generate output filename if not provided
if (!params.outputFile) {
const promptPrefix = params.prompt.substring(0, 20).replace(/[^\w]/g, '_');
params.outputFile = `video_${promptPrefix}_${Date.now()}`;
}

const result = await this.videoApi.generateVideo(params);
if (params.async_mode) {
const result = await this.videoApi.generateVideo({ ...params, asyncMode });
if (asyncMode) {
return {
content: [
{
Expand Down
46 changes: 46 additions & 0 deletions tests/rest-async-video.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { MediaService } from '../build/services/media-service.js';

class FakeVideoAPI {
constructor() {
this.postCalls = [];
this.getCalls = [];
}

async post(endpoint, body) {
this.postCalls.push({ endpoint, body });
return { task_id: 'task-123' };
}

async get(endpoint) {
this.getCalls.push(endpoint);
throw new Error('video polling should not start for async requests');
}

getResourceMode() {
return 'url';
}
}

test('REST async_mode returns the task id without polling', async () => {
const api = new FakeVideoAPI();
const service = new MediaService(api);
await service.initialize({ apiKey: 'test-key', resourceMode: 'url' });

const result = await service.generateVideo({
prompt: 'a test video',
async_mode: true,
});

assert.deepEqual(result, {
content: [
{
type: 'text',
text: 'Success. Video generation task submitted: Task ID: task-123. Please use `query_video_generation` tool to check the status of the task and get the result.',
},
],
});
assert.equal(api.postCalls.length, 1);
assert.equal(api.getCalls.length, 0);
});