Answer: You define a server plugin using Plugin.define with an id and a setup(ctx) function; the setup lifecycle runs when the plugin loads and may return a cleanup function that runs when the plugin unloads. Status: Confirmed Entry: tool/opencode/agents-and-automation/server-plugin-lifecycle-setup Revision: 1 Digest: sha256:bb80883659a1e375 (changes whenever the content changes) Timeline: Created: 2026-11-13T13:28:55Z Last changed: 2026-11-13T13:28:55Z Last verified: 2026-11-20T13:44:45Z Next check: 2026-11-21T13:44:45Z Check interval: Daily Expected change: No fixed schedule Data: Field | Value | Source | As of definition method | Plugin .define | [1] | 2026-11-20 load lifecycle | setup runs when the plugin loads | [1] | 2026-11-20 cleanup function | return () => console .log ( "unloaded" ) | [1] | 2026-11-20 cleanup timing | runs when the plugin unloads | [1] | 2026-11-20 Sources: [1] opencode. "Server | OpenCode". https://opencode.ai/docs/build/plugins/server/ Role: primary. Accessed: 2026-11-20T13:44:45Z Quote: "export default Plugin .define ({ id : "example" , setup (ctx) {" (Supports: definition method) Quote: "Lifecycle setup runs when the plugin loads." (Supports: load lifecycle) Quote: "return () => console .log ( "unloaded" )" (Supports: cleanup function) Quote: "It may return a cleanup function that runs when the plugin unloads." (Supports: cleanup timing) Snapshot SHA-256: a2439abc14eb4e3a16653b38915234a364cf331bfe60c4f3f6dfe005622e7846 (captured 2026-11-13T03:55:57Z) Verification: Method: Automated extraction from [1] with deepseek-flash (JSON output, thinking disabled). Every value was checked by code to appear verbatim in a quoted passage of [1]. On this check the text of [1] was unchanged since 2026-11-13T13:28:55Z, so the values verified then were kept. Cross-check: not-available at 2026-11-20T13:44:45Z. No independent second source is configured for this entry Last successful check: 2026-11-20T13:44:45Z Last attempt: 2026-11-20T13:44:45Z Notes and limitations: Extracted automatically from a single official source (section "Lifecycle"). Related: https://latestn.com/tool/opencode/agents-and-automation/server-plugin-transforms How do I transform or modify opencode behavior using the plugin API? https://latestn.com/tool/opencode/commands-and-shortcuts/attachment-syntax How do I describe an attachment in opencode prompt syntax? https://latestn.com/tool/opencode/commands-and-shortcuts/auth-login-cli How do I log in to a provider from the opencode CLI without opening the TUI? https://latestn.com/tool/opencode/agents-and-automation/export-effect-plugin How do I export an Effect plugin so opencode loads it automatically? https://latestn.com/tool/opencode/agents-and-automation/run-non-interactive How do I run opencode non-interactively for scripts or CI? https://latestn.com/tool/opencode/agents-and-automation/select-agent How do I switch between or select different opencode agents? https://latestn.com/tool/ All entries in AI tools Change log: Time | Event | Change | Source 2026-11-13T13:28:55Z | added | Entry created | — Changes for this entry as a feed: https://latestn.com/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup/latest Challenge this entry: Open challenges: None How: If a value here is wrong or out of date, send what is wrong and a link to evidence. Challenges are queued and checked against the evidence; they do not change the entry by themselves. If the entry changes as a result, the change log says so. Form: https://latestn.com/dispute/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup POST: https://latestn.com/dispute/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup with claim, evidence_url, and optionally field and contact (form-encoded or JSON) GET: https://latestn.com/dispute/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup?claim={what is wrong}&evidence_url={link} How to cite: LatestN. (2026). How do I define a server plugin in opencode and run code when it loads? Revision 1, last verified 2026-11-20T13:44:45Z. https://latestn.com/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup (accessed 2026-11-21T09:53:46Z). @misc{LatestN_tool_opencode_agents_and_automation_server_plugin_lifecycle_setup_r1, title = {How do I define a server plugin in opencode and run code when it loads?}, author = {{LatestN}}, year = {2026}, howpublished = {\url{https://latestn.com/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup}}, note = {Revision 1; digest sha256:bb80883659a1e375; last verified 2026-11-20T13:44:45Z; accessed 2026-11-21T09:53:46Z} } Reuse: License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/) Attribution: Copying and adapting are allowed, including commercially, if you credit LatestN and link to the entry you used. Each entry ends with a ready-made citation. URL: https://latestn.com/tool/opencode/agents-and-automation/server-plugin-lifecycle-setup