<span id="发布工作流-api-服务"></span>

# Publish Workflow API Services

Publish a workflow as a dynamic API service (`dynamic_service`) bound to an HTML interaction page stored in a workspace volume for clients that support MCP Apps.

(sdk-ai-studio-workflow-service-flow)=
<span id="任务流程"></span>

## Task flow

1. Upload the interactive HTML file to a workspace volume and obtain its file ID (`HTML_FILE_ID`).
2. Prepare a tested native MOWL workflow definition together with JSON Schema files matching its inputs and outputs.
3. Deploy the workflow in `dynamic_service` mode with the service name, input and output schemas, result mode, and HTML UI resource reference.
4. Save the returned workflow ID and inspect any deployment warnings; include the UI resource reference again whenever updating the dynamic service configuration to retain the page binding.

(sdk-ai-studio-workflow-service-prepare)=
<span id="准备"></span>

## Prerequisites

1. Complete the endpoint, personal access token, and workspace setup in [Use the SDK](../../../quickstart/ai-studio/sdk.md).
2. **Workflow definition and schemas** (`WORKFLOW_FILE`, `INPUT_SCHEMA_FILE`, `OUTPUT_SCHEMA_FILE`): prepare a tested native MOWL JSON file and JSON Schema files matching the workflow's inputs and outputs.
3. **HTML file ID and existing workflow ID**: upload the HTML file to a workspace volume as shown in [Manage Volumes and Files](../资源中心/管理卷和文件.md#sdk-ai-studio-volume-file-upload) and save the returned file ID (`HTML_FILE_ID`). When updating an existing service, obtain its workflow ID (`WORKFLOW_ID`) as shown in [Read Resource Inventories](../资源中心/读取资源清单.md).

(sdk-ai-studio-workflow-service-publish)=
<span id="发布动态服务"></span>

## Publish the dynamic service

Pass the workflow definition, input and output JSON Schema contents, and HTML file ID to deploy the service. Leave `WORKFLOW_ID` unset when publishing a new service.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
import json
import os
from pathlib import Path
import moi_product_sdk as sdk

client = sdk.new_with_personal_access_token(
    os.environ["PRODUCT_API_BASE_URL"], os.environ["PRODUCT_API_KEY"]
)
workspace = client.workspace(os.environ["WORKSPACE_ID"])
definition = json.loads(Path(os.environ["WORKFLOW_FILE"]).read_text())
workflow, deployment = workspace.deploy_workflow(
    "Document service", "", os.environ.get("WORKFLOW_ID", ""),
    sdk.with_workflow_mowl(definition),
    sdk.with_workflow_execution_mode("dynamic_service"),
    sdk.with_workflow_dynamic_service(
        service_name="document_service",
        input_schema=Path(os.environ["INPUT_SCHEMA_FILE"]).read_text(),
        output_schema=Path(os.environ["OUTPUT_SCHEMA_FILE"]).read_text(),
        result_mode="oneshot",
        ui_resource={"file_id": os.environ["HTML_FILE_ID"], "mime_type": "text/html"},
    ),
)
print("Workflow:", workflow.id)
print("Warnings:", deployment.warnings)
```

::::

::::{tab-item} Go
:sync: go

```go
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	sdk "github.com/matrixorigin/matrixflow/sdk/go-sdk"
)

func main() {
	ctx := context.Background()
	client, err := sdk.NewWithPersonalAccessToken(
		os.Getenv("PRODUCT_API_BASE_URL"),
		os.Getenv("PRODUCT_API_KEY"),
	)
	if err != nil {
		panic(err)
	}
	workspace, err := client.Workspace(os.Getenv("WORKSPACE_ID"))
	if err != nil {
		panic(err)
	}
	definition, err := os.ReadFile(os.Getenv("WORKFLOW_FILE"))
	if err != nil {
		panic(err)
	}
	inputSchema, err := os.ReadFile(os.Getenv("INPUT_SCHEMA_FILE"))
	if err != nil {
		panic(err)
	}
	outputSchema, err := os.ReadFile(os.Getenv("OUTPUT_SCHEMA_FILE"))
	if err != nil {
		panic(err)
	}
	workflow, deployment, err := workspace.DeployWorkflow(
		ctx,
		"Document service",
		"",
		os.Getenv("WORKFLOW_ID"),
		sdk.WithWorkflowMOWL(json.RawMessage(definition)),
		sdk.WithWorkflowExecutionMode("dynamic_service"),
		sdk.WithWorkflowDynamicService(sdk.WorkflowDynamicServiceParam{
			ServiceName:  "document_service",
			InputSchema:  string(inputSchema),
			OutputSchema: string(outputSchema),
			ResultMode:   "oneshot",
			UIResource: &sdk.WorkflowUIResourceParam{
				FileID:   os.Getenv("HTML_FILE_ID"),
				MimeType: "text/html",
			},
		}),
	)
	if err != nil {
		panic(err)
	}
	fmt.Println("Workflow:", workflow.ID())
	fmt.Println("Warnings:", deployment.GetWarnings())
}
```

::::

:::::

Save the returned workflow ID, inspect the workflow details to confirm the dynamic service configuration and invocation endpoint, and verify both a service call and page rendering in your target client. Standard MCP clients that support tool invocation only do not render MCP Apps interaction pages.

(sdk-ai-studio-workflow-service-update)=
<span id="更新页面绑定"></span>

## Update a page binding

When explicitly updating a workflow's dynamic service configuration, omitting the UI resource clears the existing HTML page binding; include the existing `file_id` and `mime_type` whenever you want to retain the bound page. To publish a single operator node as a headless API service, see [Manage WorkItem Services](管理WorkItem服务.md).

(sdk-ai-studio-workflow-service-next)=
<span id="下一步"></span>

## Next steps

- [Manage WorkItem Services](管理WorkItem服务.md)
- [Run and Track Workflows](运行并跟踪工作流.md)
