/kortex:main-ktsfrom kortex
Creates, writes, and runs standalone executable Kotlin scripts (.main.kts) with dependency declarations, shebang execution, and compiler annotations. Use for one-off automation, CLI utilities, and quick API calls without a build system.
Ships in kortex. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Use this skill to generate and run executable Kotlin .main.kts scripts: single-file programs that declare their own
dependencies, run directly from the shell, and need no build file.
This is the kotlin script runner, not Kotlin Toolchain, Gradle, or Maven. Do not add module.yaml, build.gradle, or
pom.xml. If the task needs a real multi-file project or published artifact, it is not a .main.kts task.
./script.main.kts or kotlin script.main.kts.Prefer a real Kotlin Toolchain or Gradle module when the code grows multiple source files, needs incremental builds, or ships as a library or application artifact.
Before writing, confirm:
.main.kts).args, env vars, stdin).@file: annotations must come first, before any import or code. Order them: Repository, DependsOn, Import,
CompilerOptions, OptIn.
#!/usr/bin/env kotlin
/**
* [What the script does.]
*
* Usage:
* ./script.main.kts <args>
*/
// Custom repositories. If any @file:Repository is present, list every repository needed.
// Maven Central is not kept implicitly once custom repositories are declared.
@file:Repository("https://repo.maven.apache.org/maven2/")
@file:Repository("https://jitpack.io")
// Maven dependencies
@file:DependsOn("group:artifact:version")
// Compose other scripts (optional)
@file:Import("other.main.kts")
// Compiler flags (optional)
@file:CompilerOptions("-jvm-target", "21")
// Opt-ins (optional)
@file:OptIn(ExperimentalStdlibApi::class)
import ...
// Top-level script code runs directly; `args` is available.
.main.kts when the script needs the main.kts script definition, especially
@file:DependsOn, @file:Repository, @file:Import, or compiler options. A plain .kts script run with
kotlin script.kts arg1 can still read args; do not rename solely for CLI arguments.#!/usr/bin/env kotlin enables ./script.main.kts. After writing, suggest chmod +x script.main.kts.kotlin script.main.kts arg1 arg2 or, once executable, ./script.main.kts arg1 arg2.kotlinr, not kotlin. On
those versions run kotlinr script.main.kts and use #!/usr/bin/env kotlinr in the shebang. Earlier versions keep
using kotlin.args: Array<String> variable. Validate and fail fast with error(...)
or kotlin.system.exitProcess(1).@file:DependsOn("group:artifact:version") resolves from Maven Central by default when no
@file:Repository annotations are present. If you add any repository annotation, list every repository the script
needs, including Maven Central (https://repo.maven.apache.org/maven2/) plus custom repositories such as Google or
JitPack. Version is required; do not rely on ranges.@file:Import("other.main.kts") inlines another script so it can share top-level declarations.@file: annotations change. The
cache lives at $KOTLIN_MAIN_KTS_COMPILED_SCRIPTS_CACHE_DIR, or ~/Library/Caches/main.kts.compiled.cache on macOS by
default. Disable by setting that variable to an empty value if you need a clean compile.@file: annotations only — not
-Xplugin or other flags passed to the runner. So toggling -Xplugin on an unchanged file does not force a recompile:
a run with the serialization plugin caches serializer-bearing bytecode that a later run without the plugin silently
reuses (and vice versa). When testing whether a compiler plugin actually applies, clear the cache first — otherwise a
stale entry masks the real result. This is a genuine debugging trap: an @Serializable script can appear to work with
no plugin simply because an earlier plugged-in run populated the cache.kotlin vs kotlinc -scriptBoth compile and execute a .main.kts — since Kotlin 1.3.70 the runner handles scripts "the same way as
kotlinc -script". They differ in the interface, how script arguments are passed, and how you supply compiler options:
kotlin script.main.kts args... | kotlinc -script script.main.kts -- args... | |
|---|---|---|
| What it is | Runner wrapper - detects the script, compiles, runs. Best for CLI/shebang. | Compiler CLI - you state explicitly that it is a script. |
Script args | Everything after the file name becomes args. | Use -- to split script args from compiler options. |
| Compiler plugin | kotlin -Xplugin=... script.main.kts | kotlinc -script -Xplugin=... script.main.kts |
Both apply -Xplugin at the script's compile phase — this is how @Serializable is made to work (see Serialization
formats). For debugging compiler-plugin problems, prefer kotlinc -script: you work with the compiler and its options
directly, without the runner's own command-line parsing in between.
For .main.kts in CI, use Setup Kotlin Scripting, also available on
GitHub Marketplace. It installs Java and the Kotlin
compiler, verifies the compiler archive checksum, and caches the archive, Maven dependencies, and compiled scripts.
Check out the repository before setup so the action can hash the scripts. Run scripts in subsequent steps:
steps:
- uses: actions/checkout@v7
- uses: Heapy/setup-main-kts@b0858237748f325b825e752bbac2ec2eb1fb9419 # v1.0.1
with:
kotlin-version: '2.4.20'
java-version: '25'
- run: kotlinr scripts/check.main.kts
Keep the project's Kotlin/JDK version pins when adopting the action. It exports JAVA_HOME, KOTLIN_HOME, and
KOTLIN_MAIN_KTS_COMPILED_SCRIPTS_CACHE_DIR, adds the compiler to PATH, and provides kotlinr even for older Kotlin
releases through a compatibility shim. To reuse Java from an earlier actions/setup-java step, set setup-java: 'false';
an existing JAVA_HOME is required.
cache-dependency-path defaults to **/*.main.kts. Extend its newline-separated patterns to include imported scripts,
local JARs, and compiler plugins that affect compilation.cache-compiler-options and pass them to the runner.
This input only invalidates the cache; it does not configure compilation.cache-compiled-scripts: 'false' and clear the local compiled cache before running the changed configuration.toolchain-java-home: 'true' also exports KOTLIN_CLI_JAVA_HOME
for its CLI JVM; enable it only with a compatible JDK. This is separate from the Toolchain project's JDK selection.See the action's README for all inputs, cache controls, and platform requirements.
| Library | Coordinate | Use case |
|---|---|---|
| ktor-client | io.ktor:ktor-client-cio-jvm:3.5.1 | HTTP client (async; see below) |
| kotlinx-coroutines | org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.11.0 | async / concurrency |
| clikt | com.github.ajalt.clikt:clikt-jvm:5.1.0 | CLI argument parsing |
| kotlinx-html | org.jetbrains.kotlinx:kotlinx-html-jvm:0.12.0 | HTML generation |
Versions move; confirm the latest stable coordinate on central.sonatype.com before pinning if the script must stay current.
Use the -jvm artifact for Kotlin Multiplatform libraries. @file:DependsOn resolves through a Maven resolver that
reads .pom files and ignores Gradle Module Metadata (the .module files). Most KMP libraries here (Ktor, clikt, and the
format libraries below) publish their bare coordinate as an empty umbrella jar with no JVM classes — the bytecode
lives in the -jvm platform artifact — so the bare coordinate resolves but yields unresolved reference at compile
time. Depend on io.ktor:ktor-client-cio-jvm, not io.ktor:ktor-client-cio. The -jvm leaf always works, so it is the
one uniform rule; a library already pinned to a -jvm (or plain-JVM) leaf — like kotlinx-html-jvm — needs no change.
The exception proves the mechanism: kotlinx-serialization and kotlinx-coroutines publish their root as a POM that
declares a compile dependency on their own -jvm artifact, which the resolver follows — so kotlinx-serialization-json
(no -jvm) also resolves. It is not that these libraries are special to the runner; it is that they redirect via POM
where the umbrella libraries do not. Verified on Kotlin 2.4.0 by inspecting each root POM and jar; -jvm is still the
safe uniform choice.
These build on kotlinx.serialization — in a real module you annotate a data class with @Serializable once and swap the
format backend (JSON, YAML, TOML, XML, CSV) over the same model.
@Serializableneeds the serialization compiler plugin, passed on the command line. A barekotlin script.main.ktsdoes not apply it, and@file:CompilerOptions("-Xplugin=…")does not enable it either — so encode/decode throwsSerializer for class … is not foundat runtime. It does work when you pass the plugin as a-Xpluginflag to the runner or compiler (verified on Kotlin 2.4.0):kotlin -Xplugin="$KOTLIN_HOME/lib/kotlinx-serialization-compiler-plugin.jar" script.main.kts. To keep./script.main.ktsself-contained via a shebang, bake that flag in with a shebang form below (env -Sor a shell polyglot); or skip codegen and walk the parsed tree (JSON shown below). When toggling the plugin on and off to check whether it applied, clear the compiled-script cache between runs — it is not keyed by-Xplugin, so a stale entry will mask the result (see Caching).
| Format | Coordinate (-jvm leaf, see the KMP note above) |
|---|---|
| JSON | org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.11.0 |
| YAML | io.heapy.kotaml:kotaml-jvm:0.110.0 |
| TOML | com.akuleshov7:ktoml-core-jvm:0.7.1 |
| XML | io.github.pdvrieze.xmlutil:serialization-jvm:1.0.0 |
| CSV | app.softwork:kotlinx-serialization-csv-jvm:0.0.23 |
JSON in a script works without the plugin if you skip @Serializable and walk the parsed tree instead:
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.11.0")
import kotlinx.serialization.json.*
val root = Json.parseToJsonElement(jsonText) // no @Serializable, no compiler plugin
val name = root.jsonObject["name"]!!.jsonPrimitive.content
Self-contained shebang version. Passing -Xplugin on the command line works, but then ./script.main.kts no longer
runs standalone. To bake the plugin into the file so it executes directly, use one of two shebang forms. Both need
KOTLIN_HOME to point at the compiler, and both must be run as ./script.main.kts, not kotlin script.main.kts —
go through the runner and the shebang is never read, so -Xplugin is skipped and @Serializable fails at runtime again.
Option A — env -S (needs GNU coreutils 8.30+/2018 or BSD env). env -S splits the shebang string and hands the
runner kotlin -Xplugin=… script.main.kts args… — exactly the argv documented above. But ${VAR} expansion inside a
real shebang is platform-split, and the difference decides whether the file runs at all:
#!/usr/bin/env -S kotlin -Xplugin=${KOTLIN_HOME}/lib/kotlinx-serialization-compiler-plugin.jar
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.11.0")
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable data class Data(val a: Int, val b: String)
println(Json.encodeToString(Data(42, "str"))) // prints {"a":42,"b":"str"}
${KOTLIN_HOME} expands; on macOS it does not — the kernel decides, not env. Linux hands env the whole
shebang tail as one argument, so env -S splits and expands it. macOS/XNU splits the #! line on whitespace before
env runs, so env -S only sees the first token as its -S operand; ${KOTLIN_HOME} arrives as a separate,
already-split argv element that env passes through verbatim. The literal string reaches the compiler, which dies with
plugin classpath entry points to a non-existent location: ${KOTLIN_HOME}/…. Tested on macOS + Kotlin 2.4.0 (fails);
env -S expands the variable only when called from a shell, not from a shebang. The Linux path is inferred from its
single-arg shebang contract, not run in this session.${KOTLIN_HOME} expands; $KOTLIN_HOME fails with
only ${VARNAME} expansion is supported — -S's own parser, not the shell.-Xplugin=/…/lib/kotlinx-serialization-compiler-plugin.jar) — that form
is verified working. The cost is length: the kernel measures the shebang against BINPRM_BUF_SIZE (128 bytes before
Linux 5.1, 256 after), and a full jar path runs ~126 bytes even from a short prefix — brushing the old cap. The variable
was what kept the line short, and that is exactly what macOS won't expand, so there is no headroom to gain here.kotlinr, so the shebang becomes #!/usr/bin/env -S kotlinr -Xplugin=….Option B — shell/Kotlin polyglot (portable; bare POSIX sh). Uglier, but needs no env -S, so it runs on any POSIX
shell:
#!/bin/sh
//usr/bin/env kotlinc -script -Xplugin="$KOTLIN_HOME/lib/kotlinx-serialization-compiler-plugin.jar" -- "$0" "$@"; exit $?
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.11.0")
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable data class Data(val a: Int, val b: String)
println(Json.encodeToString(Data(42, "str"))) // prints {"a":42,"b":"str"}
Run it as ./script.main.kts (or sh script.main.kts). Line 2 is the pivot: the shell reads //usr/bin/env… as the
path /usr/bin/env and execs the compiler, while Kotlin reads the same line as a // comment. Keep that exec on a single
// line — a /* written after // stays inside the line comment and never opens a block comment, so the shell lines
leak into Kotlin and fail to compile (the common trap).
Both examples use the plain kotlinx-serialization-json coordinate because its root POM redirects to -jvm (see the KMP
note); an umbrella-only library would still need its -jvm leaf here. Because Option B expands $KOTLIN_HOME in a real
shell line, it keeps a variable path working everywhere, macOS included — so it is the portable choice for a single file
that must run on both. Use Option A's variable form on Linux; on macOS use Option B or hardcode the jar path. Even so, a
real Gradle/Kotlin module is the clean
answer; use either shebang only for a genuinely single-file need.
Each engine pulls in ktor-client-core-jvm transitively, so depend on one engine alone — pick it:
| Engine | Notes |
|---|---|
io.ktor:ktor-client-cio-jvm:3.5.1 | Pure-Kotlin coroutine engine, no extra deps. Good default. |
io.ktor:ktor-client-java-jvm:3.5.1 | JDK 11+ java.net.http; HTTP/2, no third-party deps. |
io.ktor:ktor-client-okhttp-jvm:3.5.1 | OkHttp-backed; HTTP/2, connection pooling. |
io.ktor:ktor-client-apache5-jvm:3.5.1 | Apache HttpClient 5. |
Plugins are separate artifacts; add them alongside the engine as needed:
| Plugin | Purpose |
|---|---|
io.ktor:ktor-client-content-negotiation-jvm:3.5.1 | (De)serialize bodies by Content-Type. |
io.ktor:ktor-client-logging-jvm:3.5.1 | Request/response logging. |
io.ktor:ktor-client-auth-jvm:3.5.1 | Basic / Bearer (with token refresh) / Digest auth. |
io.ktor:ktor-client-encoding-jvm:3.5.1 | gzip/deflate compression handling. |
io.ktor:ktor-client-websockets-jvm:3.5.1 | WebSocket support. |
Content negotiation also needs a converter — io.ktor:ktor-serialization-kotlinx-json-jvm:3.5.1 (note the
ktor-serialization-* prefix, not ktor-client-*). It relies on @Serializable, so the codegen caveat above applies
in a plain script.
Use the JDK's ProcessBuilder — it is enough for scripts and needs no dependency.
.main.kts extension.chmod +x <name>.main.kts if they want ./<name>.main.kts execution.#!/usr/bin/env kotlin
/**
* Fetches a URL and prints the response body.
*
* Usage:
* ./fetch.main.kts https://api.example.com/data
*/
@file:DependsOn("io.ktor:ktor-client-cio-jvm:3.5.1")
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.isSuccess
import kotlinx.coroutines.runBlocking
import kotlin.system.exitProcess
val url = args.firstOrNull() ?: run {
System.err.println("Usage: fetch.main.kts <url>")
exitProcess(1)
}
runBlocking {
HttpClient(CIO).use { client ->
val response = client.get(url)
if (!response.status.isSuccess()) {
System.err.println("HTTP ${response.status}")
exitProcess(1)
}
println(response.bodyAsText())
}
}
.kts file that is not .main.kts can still see args, but it will not resolve main.kts annotations such as
@file:DependsOn; rename only when those script-definition features are needed.@file: annotations after the first import or statement are ignored or fail — keep them at the top.@file:DependsOn break resolution; always pin group:artifact:version.@file:Repository(...) before @file:DependsOn(...) and
include Maven Central explicitly too if any dependency still comes from Central.-jvm artifact — unresolved reference
despite a correct @file:DependsOn usually means a missing -jvm suffix (see Common Dependencies).@Serializable codegen is off by default in .main.kts (the plugin is not applied), so it fails at runtime, not
compile time. Enable it by passing -Xplugin=…serialization… to the runner/compiler, parse JSON via JsonElement, or
move the logic to a real module.const val is rejected at the top level of a script — use a plain val (top-level script declarations are not inside
an object).What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
npx claudepluginhub heapy/kortex --plugin kortex5 skills in this plugin
/jshellUse when running Java snippets in JShell or using JShell as a Java REPL or scratchpad./kotlin-toolchainUse when working with JetBrains Kotlin Toolchain v0.14.x, formerly Amper, including module.yaml, project.yaml, module templates, nested templates, libs.versions.toml, the kotlin CLI wrapper, `//` project paths, Kotlin/JVM, Android, iOS, Kotlin Multiplatform, Kotlin/JS, Kotlin/Wasm (wasm-js, wasm-wasi), Kotlin/Native, Apple platform support and deprecations, macOS/Xcode compatibility, ARM64 hosts and cross-compilation, GitHub Actions runners, server-side apps, dependencies, testing, SwiftPM dependencies, KMP library publishing, Maven Central, TestFlight, Android desugaring, download mirrors, build plugins, built-in technologies (Compose, Ktor, Spring Boot, serialization, kotlinx.rpc, DataFrame, Power Assert, KSP, Lombok), Maven and Gradle migration, JDK provisioning, and toolchain caches./