Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ai/mcp-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The MCP server is a preview feature since Camel 4.22.
1. One `ai-tool` route that answers a fixed text, with the four `camel.server.mcp-*` properties; run it and see
the server line in the log, then `initialize` and `tools/list` with `curl`.
2. Add a parameter and use `${header.sku}` in the answer; call it with `tools/call`.
3. Answer from `stock.json` with `jsonpath` and add the unknown case.
3. Answer from `stock.json`: unmarshal it, find the SKU with Groovy and add the unknown case.
4. Add the second tool and connect a real agent.

## Try changing
Expand Down
13 changes: 8 additions & 5 deletions ai/mcp-server/mcp-server.camel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
expression:
constant:
expression: resource:file:stock.json
- unmarshal:
json:
library: Jackson
# the item of that SKU as a map, or null when there is none
- setBody:
expression:
jsonpath:
expression: "$[?(@.sku == '${header.sku}')]"
resultType: java.util.List
groovy:
expression: "body.find { it.sku == headers.sku }"
- choice:
when:
- expression:
simple:
expression: "${body.size()} == 0"
expression: "${body} == null"
steps:
- setBody:
expression:
Expand All @@ -37,7 +40,7 @@
- setBody:
expression:
simple:
expression: "${body[0][qty]} ${header.sku} in stock"
expression: "${body[qty]} ${header.sku} in stock"
- log:
message: "Tool stock_level(${header.sku}): ${body}"

Expand Down
2 changes: 1 addition & 1 deletion ai/mcp-server/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"languages": [
"simple",
"jsonpath",
"groovy",
"constant"
],
"dataformats": [
Expand Down
9 changes: 5 additions & 4 deletions camel-jbang-example-catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
],
"languages": [
"simple",
"jsonpath",
"groovy",
"constant"
],
"dataformats": [
Expand Down Expand Up @@ -548,7 +548,7 @@
],
"languages": [
"simple",
"jsonpath"
"groovy"
],
"dataformats": [
"json"
Expand Down Expand Up @@ -589,11 +589,12 @@
"setBody",
"setHeader",
"choice",
"unmarshal",
"marshal"
],
"languages": [
"constant",
"jsonpath",
"groovy",
"simple"
],
"dataformats": [
Expand Down Expand Up @@ -728,7 +729,7 @@
],
"languages": [
"simple",
"jsonpath",
"groovy",
"constant"
],
"dataformats": [
Expand Down
15 changes: 7 additions & 8 deletions connect/http-client/http-client.camel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
expression:
constant:
expression: resource:file:stock.json
- unmarshal:
json:
library: Jackson
# the item of that SKU as a map, or null when there is none
- setBody:
expression:
jsonpath:
expression: "$[?(@.sku == '${header.sku}')]"
resultType: java.util.List
groovy:
expression: "body.find { it.sku == headers.sku }"
- choice:
when:
- expression:
simple:
expression: "${body.size()} == 0"
expression: "${body} == null"
steps:
- setHeader:
name: CamelHttpResponseCode
Expand All @@ -41,10 +44,6 @@
expression: '{"error": "unknown sku ${header.sku}"}'
otherwise:
steps:
- setBody:
expression:
simple:
expression: "${body[0]}"
- marshal:
json:
library: Jackson
Expand Down
2 changes: 1 addition & 1 deletion connect/http-client/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"languages": [
"simple",
"jsonpath"
"groovy"
],
"dataformats": [
"json"
Expand Down
10 changes: 5 additions & 5 deletions connect/stock-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ Then, in another terminal, call it with `curl` as above. Stop it with `ctrl` + `
item. Each operation hands over to a `direct` route. The CLI starts the HTTP server on port 8080 because the
file uses the REST DSL; `application.properties` sets the port.
- The list route sets the body to `resource:file:stock.json`, the file next to the route, and the content type.
- The item route loads the same file and picks the item with a `jsonpath` filter, `$[?(@.sku == '...')]`, in which
the `{sku}` path parameter arrives as the header `sku`. The result is a list: empty means 404 with a small error
body, otherwise the first element is marshalled back to JSON.
- The item route loads the same file, unmarshals it into a list of maps, and picks the item with one line of Groovy,
`body.find { it.sku == headers.sku }`, in which the `{sku}` path parameter arrives as the header `sku`. The result is
the item or null: null means 404 with a small error body, otherwise the item is marshalled back to JSON.
- `CamelHttpResponseCode` is the header that sets the status code; 200 is the default.

## Build it step by step

1. A `rest` with one `get` that answers `resource:file:stock.json`; run it and `curl localhost:8080/stock`.
2. Add the `/{sku}` operation and log `${header.sku}` to see the path parameter arrive.
3. Filter the file with `jsonpath` and return the first element; `curl` a known SKU.
4. Add the `choice` for the empty result and the 404.
3. Unmarshal the file and find the item with Groovy, `body.find { it.sku == headers.sku }`; `curl` a known SKU.
4. Add the `choice` for the null result and the 404.

## Try changing

Expand Down
3 changes: 2 additions & 1 deletion connect/stock-api/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"setBody",
"setHeader",
"choice",
"unmarshal",
"marshal"
],
"languages": [
"constant",
"jsonpath",
"groovy",
"simple"
],
"dataformats": [
Expand Down
15 changes: 7 additions & 8 deletions connect/stock-api/stock-api.camel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@
expression:
constant:
expression: resource:file:stock.json
- unmarshal:
json:
library: Jackson
# the item of that SKU as a map, or null when there is none
- setBody:
expression:
jsonpath:
expression: "$[?(@.sku == '${header.sku}')]"
resultType: java.util.List
groovy:
expression: "body.find { it.sku == headers.sku }"
- choice:
when:
- expression:
simple:
expression: "${body.size()} == 0"
expression: "${body} == null"
steps:
- setHeader:
name: CamelHttpResponseCode
Expand All @@ -59,10 +62,6 @@
expression: '{"error": "unknown sku ${header.sku}"}'
otherwise:
steps:
- setBody:
expression:
simple:
expression: "${body[0]}"
- marshal:
json:
library: Jackson
Expand Down
4 changes: 2 additions & 2 deletions contracts/openapi-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Stop it with `ctrl` + `c`.
`apiContextPath: openapi` serves the contract.
- Each operation is a `direct` route named after its `operationId`: `listStock`, `getStock`, `reserveStock`.
Path parameters arrive as headers, `sku` here; the request body is the message body.
- `lookup` is a helper route the two SKU operations share: it filters the stock file with `jsonpath` and
leaves the item, or null, as the body.
- `lookup` is a helper route the two SKU operations share: it unmarshals the stock file and finds the SKU
with Groovy, `body.find { it.sku == headers.sku }`, which leaves the item, or null, as the body.
- The contract check does not look at the values, so `reserveStock` runs `validate` on them and an
`onException` turns the failed predicate into a 400 with an error body, the status the contract promises.
- `CamelHttpResponseCode` sets 404 and 409; the bodies are the JSON the contract's `Error` schema describes.
Expand Down
2 changes: 1 addition & 1 deletion contracts/openapi-server/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"languages": [
"simple",
"jsonpath",
"groovy",
"constant"
],
"dataformats": [
Expand Down
25 changes: 6 additions & 19 deletions contracts/openapi-server/openapi-server.camel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,11 @@
expression:
constant:
expression: resource:file:stock.json
- unmarshal:
json:
library: Jackson
# the item of that SKU as a map, or null when there is none
- setBody:
expression:
jsonpath:
expression: "$[?(@.sku == '${header.sku}')]"
resultType: java.util.List
- choice:
when:
- expression:
simple:
expression: "${body.size()} == 0"
steps:
- setBody:
expression:
simple:
expression: "${null}"
otherwise:
steps:
- setBody:
expression:
simple:
expression: "${body[0]}"
groovy:
expression: "body.find { it.sku == headers.sku }"
Loading