mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat(schedules): attach MCP servers to scheduled runs
This commit is contained in:
1 parent
24e2037b73
commit
8e32507f21
20 files changed
+714
-165
No files matched your search
@@ -2,6 +2,7 @@ import { Cron } from 'croner'
|
||||
import type {
|
||||
CreateScheduleJobRequest,
|
||||
ScheduleJob,
|
||||
ScheduleMcpServer,
|
||||
ScheduleMode,
|
||||
SchedulePermissionConfig,
|
||||
ScheduleSkillMetadata,
|
||||
@@ -23,6 +24,7 @@ export interface ScheduleJobPersistenceInput {
|
||||
model: string | null
|
||||
skillMetadata: ScheduleSkillMetadata | null | undefined
|
||||
permissionConfig: SchedulePermissionConfig | null
|
||||
mcpServers: ScheduleMcpServer[]
|
||||
branch: string | null
|
||||
nextRunAt: number | null
|
||||
}
|
||||
@@ -109,6 +111,7 @@ export function buildCreateSchedulePersistenceInput(input: CreateScheduleJobRequ
|
||||
...base,
|
||||
...scheduleConfig,
|
||||
permissionConfig: input.permissionConfig ?? null,
|
||||
mcpServers: input.mcpServers ?? [],
|
||||
nextRunAt: base.enabled ? scheduleConfig.nextRunAt : null,
|
||||
}
|
||||
}
|
||||
@@ -157,6 +160,7 @@ export function buildUpdatedSchedulePersistenceInput(
|
||||
model: input.model === undefined ? existing.model : (input.model?.trim() || null),
|
||||
skillMetadata: input.skillMetadata !== undefined ? input.skillMetadata : existing.skillMetadata,
|
||||
permissionConfig: input.permissionConfig === undefined ? existing.permissionConfig : (input.permissionConfig ?? null),
|
||||
mcpServers: input.mcpServers ?? existing.mcpServers,
|
||||
branch: input.branch === undefined ? existing.branch : (input.branch?.trim() || null),
|
||||
nextRunAt,
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
type ScheduleRunTriggerSource,
|
||||
type UpdateScheduleJobRequest,
|
||||
} from '@opencode-manager/shared/types'
|
||||
import { assistantText, openCodeLocation, sessionIDFromEvent, type SessionMessageAssistant, type SessionMessageInfo } from '@opencode-manager/shared/opencode'
|
||||
import { assistantText, mcpStatusByName, openCodeLocation, sessionIDFromEvent, type SessionMessageAssistant, type SessionMessageInfo } from '@opencode-manager/shared/opencode'
|
||||
import { buildSchedulePermissionRuleset } from '@opencode-manager/shared/schemas'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import type { ScheduleJobWithRepo } from '../db/schedules'
|
||||
@@ -587,6 +587,12 @@ export class ScheduleService {
|
||||
return this.abandonCancelledStartup(repoId, jobId, run, job, repo)
|
||||
}
|
||||
|
||||
await this.connectMcpServers(runDirectory, job.mcpServers)
|
||||
|
||||
if (abort.signal.aborted) {
|
||||
return this.abandonCancelledStartup(repoId, jobId, run, job, repo)
|
||||
}
|
||||
|
||||
const sessionTitle = buildSessionTitle(job)
|
||||
let session: { id: string }
|
||||
try {
|
||||
@@ -1265,6 +1271,53 @@ export class ScheduleService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes every MCP server attached to the job available at the run's location before the
|
||||
* session starts. Schedule-only servers are added to that location, and configured servers
|
||||
* are connected even when their config disables them; both last until OpenCode restarts.
|
||||
*/
|
||||
private async connectMcpServers(directory: string, servers: ScheduleJob['mcpServers']): Promise<void> {
|
||||
if (servers.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const api = this.openCodeClient.api
|
||||
const location = openCodeLocation(directory)
|
||||
const readStatuses = async () => mcpStatusByName((await api.mcp.list(location)).data)
|
||||
|
||||
try {
|
||||
await Promise.all(servers.flatMap((server) =>
|
||||
server.config ? [api.mcp.add({ server: server.name, config: server.config, ...location })] : [],
|
||||
))
|
||||
|
||||
const statuses = await readStatuses()
|
||||
const missing = servers.filter((server) => !statuses[server.name]).map((server) => server.name)
|
||||
if (missing.length > 0) {
|
||||
throw new ScheduleServiceError(`MCP servers are not configured for this location: ${missing.join(', ')}`, 400)
|
||||
}
|
||||
|
||||
await Promise.all(servers
|
||||
.filter((server) => statuses[server.name]?.status !== 'connected')
|
||||
.map((server) => api.mcp.connect({ server: server.name, ...location })))
|
||||
|
||||
const connected = await readStatuses()
|
||||
const failed = servers.flatMap((server) => {
|
||||
const status = connected[server.name]
|
||||
if (status?.status === 'connected') return []
|
||||
const reason = status && 'error' in status ? status.error : status?.status ?? 'missing'
|
||||
return [`${server.name} (${reason})`]
|
||||
})
|
||||
if (failed.length > 0) {
|
||||
throw new ScheduleServiceError(`MCP servers failed to connect: ${failed.join(', ')}`, 502)
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof ScheduleServiceError) {
|
||||
throw error
|
||||
}
|
||||
throw new ScheduleServiceError(getErrorMessage(error) || 'Failed to connect MCP servers', 502)
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveSkillAttachments(
|
||||
directory: string,
|
||||
skillMetadata: ScheduleJob['skillMetadata'],
|
||||
|
||||
Reference in new issue
Block a user