Automating SwiftUI and React Rive Mascot Code from MCP Tool Outputs
By the Charmotion team · Updated · How we write guides

Scaffold it by having the coding agent call an MCP tool that returns a finished Rive .riv file and code bundle, then generate one SwiftUI view and one React component that load that file and set its mood number. Keep the mood map in one file, and check the output with a build and a visual test.
Key takeaways
- Generate the SwiftUI and React wrappers from one shared mood map so the numbers never drift.
- Read the download links and integration notes from the tool result instead of guessing file names.
- Pin rive-ios and @rive-app/react-canvas, then read the release notes before each upgrade.
- Let the agent write glue code only; the .riv file itself is copied, never edited.
- Build both targets and render every mood once before you merge.
Already have a character or logo? See examples in real app screens: splash, onboarding, loading and empty-state screens, with the animation that plays in each.
What do MCP tool outputs contain for a Rive mascot?
A generator can only read a tool result reliably if it knows what the result contains. Charmotion's MCP server follows the same steps as its app: a finished character image (or one drawn by its AI designer), create_character_set with the animations you choose, get_character_set for download links and AI_README integration notes, and export_videos for MP4, GIF and transparent video copies. Idle is always included.
The file that matters for code is one Rive (.riv) file with three parts: CharacterViewModelMachine, a Data Binding view model with a number property named mood; CharacterStateMachine, with a number input named mood; and LaunchStateMachine, present only when launch was picked. Read field names from the tool result instead of assuming them, and copy file names exactly as they come back.
The mood numbers are fixed, so map them once and reuse the map:
| Mood | Number | Typical use |
|---|---|---|
| idle | 0 | Default loop |
| happy | 1 | Success (no success trigger exists) |
| wave | 2 | Greeting |
| thinking | 3 | Working on a request |
| sad | 4 | Error |
| loading | 5 | Waiting |
| listening | 6 | Taking input |
| talking | 7 | Responding |
| sleeping | 8 | Inactive state |
| attention | 9 | Needs the user |
Characters have no one-shot triggers, so a success is a happy and an error is a sad. Moods blend into each other. Small accents such as sleeping Z's, thinking dots, the attention mark and happy sparkles are on by default, and the boolean accents hides them.
How to scaffold SwiftUI and React Rive code automatically from MCP tool outputs?
The pipeline has three parts: an MCP client that can call the server's tools, a step that saves the returned files into your repository, and a generator prompt with a fixed contract. Keep each part small so a failure points at one place.
Add the server to the client first. A project-level Claude Code file looks like this, with the URL replaced by your server's endpoint:
{
"mcpServers": {
"example": {
"type": "http",
"url": "https://mcp.example.com/mcp"
}
}
}Cursor takes a url entry in its mcp.json, and Codex reads remote HTTP servers from config.toml. Each client's documentation covers its own format and sign-in step.
- Call the tool that creates the character set, passing the animations you need.
- Call the tool that returns the files, and save the .riv, the code bundle and the integration notes into one folder such as assets/mascot.
- Have the agent read the integration notes first, then write one SwiftUI view and one React component that take a mood name.
- Put the mood map in one shared file and import it on both sides, so the numbers live in one place.
- Build both targets and render every mood before merging the change.
Two rules keep reruns stable. The agent copies binary files and never opens them as text, and it regenerates glue code only when the tool output changes. For a related setup in Claude, Cursor or VS Code, see how to animate a logo or character from Claude, Cursor or VS Code with MCP.
What does the generated SwiftUI side need?
Add the rive-ios package with Swift Package Manager. Rive's Apple runtime guide names the RiveRuntime product, gives 6.24.0 as the minimum version, and lists iOS 14.0, visionOS 1.0, tvOS 16.0, macOS 13.1 and Mac Catalyst 14.0 as minimum targets. The same guide says CocoaPods is in maintenance mode, so generated projects should use SPM.
A minimal view loads the bundled file and calls .view() in its body:
import SwiftUI
import RiveRuntime
struct MascotView: View {
@StateObject private var rive = RiveViewModel(fileName: "character")
var body: some View {
rive.view()
}
}Give the mood numbers a type so a typo cannot send the wrong value:
enum Mood: Double {
case idle = 0, happy = 1, wave = 2, thinking = 3, sad = 4
case loading = 5, listening = 6, talking = 7, sleeping = 8, attention = 9
}Set the value in one function, such as setMood(_ mood: Mood), and let it call whichever input or data binding call your integration notes name. Release 6.28.0 of rive-ios added global view model support to the Swift Concurrency API and redraws views after changes to bound main and global instances. The same release deprecated DataBind, the dataBind: initializer, Rive.viewModelInstance and bindViewModelInstance, so tell the agent to avoid them and pin the package version in its prompt.
What does the generated React side need?
Rive recommends @rive-app/react-canvas for most apps, since it wraps the Canvas2D runtime, and reserves the WebGL variant for cases that need it. useRive returns a Rive instance and a component to render. useStateMachineInput takes the instance, a state machine name and an input name, and returns the input once both are ready, or null until then.
Rive's React docs still describe useStateMachineInput, so check the hook reference for the version you install before relying on it.
import { useEffect } from "react";
import { useRive, useStateMachineInput } from "@rive-app/react-canvas";
const STATE_MACHINE = "CharacterStateMachine";
export function Mascot({ mood }: { mood: number }) {
const { rive, RiveComponent } = useRive({
src: "/character.riv",
stateMachines: STATE_MACHINE,
autoplay: true,
});
const moodInput = useStateMachineInput(rive, STATE_MACHINE, "mood");
useEffect(() => {
if (moodInput) moodInput.value = mood;
}, [moodInput, mood]);
return <RiveComponent />;
}The effect keeps the input in step with the prop, and the state machine blends to the new mood. Nothing else in the component should know the numbers. For Next.js details, including server rendering, read how to use Rive in React and Next.js.
How do you check generated Rive code before it ships?
Generated glue code fails in predictable ways, so test those points rather than the look alone.
- Names match the file. The state machine and input names in code must equal what the .riv contains. A mismatch often shows up as a silent no-op, not an error.
- Every mood renders. Loop through the enum and capture one frame per mood. Compare the frames with the transparent poster images from the export.
- Accents are a deliberate choice. If your design hides the small accents, set the boolean accents to false and check the frames again.
- Both targets build. Run the Xcode build and the web build in CI with the versions you pinned.
For browser checks, Playwright can screenshot each mood in a real page. The view model side is covered in how to inspect and verify MCP-generated Rive data binding viewmodels in code.
What should the agent never generate?
- The .riv file itself. The agent copies it and never edits it. To change a mood or animation, regenerate through the tools that built it.
- New triggers. Characters have no one-shot triggers. Map success to happy and error to sad, and leave other names alone.
- Mood numbers in view code. Keep them in the shared enum or map so one change updates both targets.
- Duplicate assets on every run. Key the output folder by character set so a rerun with the same output changes nothing.
Mood changes are the most common source of drift, so review any diff that touches the map before it merges. For a wider set of mood states and how to drive them, see how to add new mood and animation states to a Rive character via MCP.
The character files in this example come from Charmotion's own MCP server, described on its MCP server page.
Scaffold SwiftUI and React Rive code from MCP tool outputs
- Connect the server. Add the MCP server to your client with its endpoint URL, and complete sign-in if the server asks for it.
- Build the character set. Call the tool that creates the character set with the animations you need.
- Save the outputs. Call the tool that returns the files, and save the .riv, the code bundle and the integration notes into one assets folder.
- Generate both targets. Have the agent read the integration notes, then write one SwiftUI view and one React component that take a mood name.
- Verify each mood. Build both targets and render every mood once before merging, comparing the frames with the exported poster images.
Frequently asked questions
Can the agent edit the .riv file to change a mood?
Not safely. A .riv file holds the artboard, state machines and view model together, and a text edit breaks it. Change behavior by regenerating the character set with the tools that built it, and let the agent change only the glue code that reads the file.
Which Rive versions should a generator pin?
Pin the Swift package to at least 6.24.0, the minimum in Rive's Apple guide, and prefer 6.28.0 or the newest release you can confirm, since it adds global view model support and automatic redraws. For React, pin @rive-app/react-canvas and read its changelog before upgrading. Run the mood render check after every upgrade.
Does a Charmotion MCP server need an API key?
No. The remote server at charmotion.app/mcp uses OAuth sign-in, so the client does not read a key from a file. Animated previews and guide search are free without an account. Designs and animation builds use your plan's credits, and video exports do not use credits.
Sources
- Rive Apple runtime guide, Rive
- rive-ios release 6.28.0, Rive (GitHub)
- Rive React runtime overview, Rive
- Rive React parameters and return values, Rive
- Connect Claude Code to tools via MCP, Anthropic
- Charmotion MCP server, Charmotion