Skip to content

Commit 3aafcdf

Browse files
committed
Docs on using ZIO
1 parent 4e0760d commit 3aafcdf

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ JSON-RPC envelopes (both for responses and requests).
8383

8484
---
8585

86+
## Using with ZIO
87+
88+
When using ZIO, you might have to explicitly state the effect type that you are using, as the Tapir-ZIO integration requires
89+
a `RIO[R, A]` effect (which is an alias for `ZIO[R, Throwable, A]`), for example:
90+
91+
```scala
92+
val myServerTool = myTool.serverLogic[[X] =>> RIO[Any, X]]: (input, headers) =>
93+
ZIO.succeed(???)
94+
```
95+
96+
---
97+
8698
## Contributing
8799

88100
Contributions are welcome! Please open issues or pull requests.

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ lazy val core: Project = (project in file("core"))
3939
"com.softwaremill.sttp.tapir" %% "tapir-core" % tapirV,
4040
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirV,
4141
"com.softwaremill.sttp.tapir" %% "tapir-apispec-docs" % tapirV,
42+
"com.softwaremill.sttp.tapir" %% "tapir-zio-http-server" % tapirV,
4243
"com.softwaremill.sttp.apispec" %% "jsonschema-circe" % "0.11.10",
4344
"org.slf4j" % "slf4j-api" % "2.0.17"
4445
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package chimp
2+
3+
//> using dep com.softwaremill.chimp::core:0.1.2
4+
//> using dep com.softwaremill.sttp.tapir::tapir-zio-http-server:1.11.33
5+
//> using dep ch.qos.logback::logback-classic:1.5.18
6+
7+
import io.circe.Codec
8+
import sttp.tapir.*
9+
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
10+
import zio.RIO
11+
import zio.ZIO
12+
import zio.ZIOAppDefault
13+
import zio.http.Server
14+
15+
case class ZioInput(a: Int, b: Int) derives Codec, Schema
16+
17+
object Main extends ZIOAppDefault:
18+
val adderTool = tool("adder")
19+
.description("Adds two numbers")
20+
.withAnnotations(ToolAnnotations(idempotentHint = Some(true)))
21+
.input[ZioInput]
22+
23+
// note that here we need to explicitly state the effect type, as the Tapir-ZIO integration requires a `RIO[R, A]`
24+
// effect (with the error channel fixed to `Throwable`)
25+
val adderServerTool = adderTool.serverLogic[[X] =>> RIO[Any, X]]: (input, _) =>
26+
ZIO.succeed(Right(s"The result is ${input.a + input.b}"))
27+
28+
val mcpServerEndpoint = mcpEndpoint(List(adderServerTool), List("mcp"))
29+
30+
val routes = ZioHttpInterpreter().toHttp(mcpServerEndpoint)
31+
32+
override def run =
33+
Server.serve(routes).provide(Server.default)

0 commit comments

Comments
 (0)