[{"data":1,"prerenderedAt":1742},["ShallowReactive",2],{"page-en-\u002Fblog\u002Fspring-ai-tool-calling-mcp":3,"related-en-\u002Fblog\u002Fspring-ai-tool-calling-mcp":1720},{"id":4,"title":5,"body":6,"date":1706,"description":1707,"extension":1708,"meta":1709,"navigation":100,"path":1710,"seo":1711,"stem":1712,"tags":1713,"translation":1718,"__hash__":1719},"pages_en\u002Fen\u002Fblog\u002Fspring-ai-tool-calling-mcp.md","Tool Calling and MCP with Spring AI",{"type":7,"value":8,"toc":1692},"minimark",[9,14,27,35,39,46,62,65,69,356,363,444,449,525,539,543,922,925,929,942,1045,1049,1052,1153,1160,1164,1167,1237,1240,1244,1247,1272,1279,1283,1349,1360,1453,1480,1487,1491,1545,1550,1575,1582,1590,1659,1665,1669,1672,1676,1688],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,17,18,22,23,26],"p",{},"On its own, an LLM only knows the data it was trained on: it can't know today's date, the status of your order or a record in your database. ",[19,20,21],"strong",{},"Tool calling"," (function calling) lets you describe certain methods in your application to the model as \"tools\" and lets the model ask for them to be called when needed. ",[19,24,25],{},"MCP (Model Context Protocol)"," makes those tools shareable across applications and AI clients through a standard protocol.",[15,28,29,30,34],{},"This post follows up on the getting started with Spring AI post and assumes ",[31,32,33],"code",{},"ChatClient"," and the basic setup are already in place.",[10,36,38],{"id":37},"_1-how-tool-calling-works","1. How tool calling works",[15,40,41,42,45],{},"The key point: ",[19,43,44],{},"the model does not run the tool itself."," The flow is:",[47,48,49,53,56,59],"ol",{},[50,51,52],"li",{},"The application sends the tools' names, descriptions and parameter schemas to the model along with the prompt.",[50,54,55],{},"If the model needs a tool to answer, it returns a \"call this tool with these arguments\" request instead of text.",[50,57,58],{},"Spring AI runs the corresponding Java method and sends the result back to the model.",[50,60,61],{},"The model uses the tool result to produce the final answer.",[15,63,64],{},"Spring AI manages this loop automatically; all you write is the method itself.",[10,66,68],{"id":67},"_2-a-first-tool-with-tool","2. A first tool with @Tool",[70,71,76],"pre",{"className":72,"code":73,"language":74,"meta":75,"style":75},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","class DateTimeTools {\n\n    @Tool(description = \"Returns the current date and time in the user's time zone\")\n    String currentDateTime() {\n        return LocalDateTime.now().atZone(ZoneId.of(\"Europe\u002FIstanbul\")).toString();\n    }\n\n    @Tool(description = \"Adds days to the given date and returns the result in ISO-8601 format\")\n    String addDays(\n            @ToolParam(description = \"ISO-8601 date, e.g. 2026-11-24\") String date,\n            @ToolParam(description = \"Number of days to add\") int days) {\n        return LocalDate.parse(date).plusDays(days).toString();\n    }\n}\n","java","",[31,77,78,95,102,133,148,199,205,210,232,243,278,310,345,350],{"__ignoreMap":75},[79,80,83,87,91],"span",{"class":81,"line":82},"line",1,[79,84,86],{"class":85},"spNyl","class",[79,88,90],{"class":89},"sBMFI"," DateTimeTools",[79,92,94],{"class":93},"sMK4o"," {\n",[79,96,98],{"class":81,"line":97},2,[79,99,101],{"emptyLinePlaceholder":100},true,"\n",[79,103,105,108,111,114,117,120,123,127,130],{"class":81,"line":104},3,[79,106,107],{"class":93},"    @",[79,109,110],{"class":85},"Tool",[79,112,113],{"class":93},"(",[79,115,116],{"class":89},"description",[79,118,119],{"class":93}," =",[79,121,122],{"class":93}," \"",[79,124,126],{"class":125},"sfazB","Returns the current date and time in the user's time zone",[79,128,129],{"class":93},"\"",[79,131,132],{"class":93},")\n",[79,134,136,139,143,146],{"class":81,"line":135},4,[79,137,138],{"class":85},"    String",[79,140,142],{"class":141},"s2Zo4"," currentDateTime",[79,144,145],{"class":93},"()",[79,147,94],{"class":93},[79,149,151,155,159,162,165,168,171,173,176,178,181,183,185,188,190,193,196],{"class":81,"line":150},5,[79,152,154],{"class":153},"s7zQu","        return",[79,156,158],{"class":157},"sTEyZ"," LocalDateTime",[79,160,161],{"class":93},".",[79,163,164],{"class":141},"now",[79,166,167],{"class":93},"().",[79,169,170],{"class":141},"atZone",[79,172,113],{"class":93},[79,174,175],{"class":157},"ZoneId",[79,177,161],{"class":93},[79,179,180],{"class":141},"of",[79,182,113],{"class":93},[79,184,129],{"class":93},[79,186,187],{"class":125},"Europe\u002FIstanbul",[79,189,129],{"class":93},[79,191,192],{"class":93},")).",[79,194,195],{"class":141},"toString",[79,197,198],{"class":93},"();\n",[79,200,202],{"class":81,"line":201},6,[79,203,204],{"class":93},"    }\n",[79,206,208],{"class":81,"line":207},7,[79,209,101],{"emptyLinePlaceholder":100},[79,211,213,215,217,219,221,223,225,228,230],{"class":81,"line":212},8,[79,214,107],{"class":93},[79,216,110],{"class":85},[79,218,113],{"class":93},[79,220,116],{"class":89},[79,222,119],{"class":93},[79,224,122],{"class":93},[79,226,227],{"class":125},"Adds days to the given date and returns the result in ISO-8601 format",[79,229,129],{"class":93},[79,231,132],{"class":93},[79,233,235,237,240],{"class":81,"line":234},9,[79,236,138],{"class":85},[79,238,239],{"class":141}," addDays",[79,241,242],{"class":93},"(\n",[79,244,246,249,252,254,256,258,260,263,265,268,271,275],{"class":81,"line":245},10,[79,247,248],{"class":93},"            @",[79,250,251],{"class":85},"ToolParam",[79,253,113],{"class":93},[79,255,116],{"class":89},[79,257,119],{"class":93},[79,259,122],{"class":93},[79,261,262],{"class":125},"ISO-8601 date, e.g. 2026-11-24",[79,264,129],{"class":93},[79,266,267],{"class":93},")",[79,269,270],{"class":85}," String",[79,272,274],{"class":273},"sHdIc"," date",[79,276,277],{"class":93},",\n",[79,279,281,283,285,287,289,291,293,296,298,300,303,306,308],{"class":81,"line":280},11,[79,282,248],{"class":93},[79,284,251],{"class":85},[79,286,113],{"class":93},[79,288,116],{"class":89},[79,290,119],{"class":93},[79,292,122],{"class":93},[79,294,295],{"class":125},"Number of days to add",[79,297,129],{"class":93},[79,299,267],{"class":93},[79,301,302],{"class":85}," int",[79,304,305],{"class":273}," days",[79,307,267],{"class":93},[79,309,94],{"class":93},[79,311,313,315,318,320,323,325,328,331,334,336,339,341,343],{"class":81,"line":312},12,[79,314,154],{"class":153},[79,316,317],{"class":157}," LocalDate",[79,319,161],{"class":93},[79,321,322],{"class":141},"parse",[79,324,113],{"class":93},[79,326,327],{"class":157},"date",[79,329,330],{"class":93},").",[79,332,333],{"class":141},"plusDays",[79,335,113],{"class":93},[79,337,338],{"class":157},"days",[79,340,330],{"class":93},[79,342,195],{"class":141},[79,344,198],{"class":93},[79,346,348],{"class":81,"line":347},13,[79,349,204],{"class":93},[79,351,353],{"class":81,"line":352},14,[79,354,355],{"class":93},"}\n",[15,357,358,359,362],{},"To give the tool to a single request, use ",[31,360,361],{},".tools(...)",":",[70,364,366],{"className":72,"code":365,"language":74,"meta":75,"style":75},"String answer = chatClient.prompt()\n        .user(\"What date is it 45 days from today?\")\n        .tools(new DateTimeTools())\n        .call()\n        .content();\n",[31,367,368,390,409,426,435],{"__ignoreMap":75},[79,369,370,373,376,379,382,384,387],{"class":81,"line":82},[79,371,372],{"class":85},"String",[79,374,375],{"class":157}," answer ",[79,377,378],{"class":93},"=",[79,380,381],{"class":157}," chatClient",[79,383,161],{"class":93},[79,385,386],{"class":141},"prompt",[79,388,389],{"class":93},"()\n",[79,391,392,395,398,400,402,405,407],{"class":81,"line":97},[79,393,394],{"class":93},"        .",[79,396,397],{"class":141},"user",[79,399,113],{"class":93},[79,401,129],{"class":93},[79,403,404],{"class":125},"What date is it 45 days from today?",[79,406,129],{"class":93},[79,408,132],{"class":93},[79,410,411,413,416,418,421,423],{"class":81,"line":104},[79,412,394],{"class":93},[79,414,415],{"class":141},"tools",[79,417,113],{"class":93},[79,419,420],{"class":153},"new",[79,422,90],{"class":141},[79,424,425],{"class":93},"())\n",[79,427,428,430,433],{"class":81,"line":135},[79,429,394],{"class":93},[79,431,432],{"class":141},"call",[79,434,389],{"class":93},[79,436,437,439,442],{"class":81,"line":150},[79,438,394],{"class":93},[79,440,441],{"class":141},"content",[79,443,198],{"class":93},[15,445,446,447,362],{},"If it should be available on every request, register it when building the ",[31,448,33],{},[70,450,452],{"className":72,"code":451,"language":74,"meta":75,"style":75},"@Bean\nChatClient chatClient(ChatClient.Builder builder, DateTimeTools dateTimeTools) {\n    return builder\n            .defaultTools(dateTimeTools)\n            .build();\n}\n",[31,453,454,462,489,497,512,521],{"__ignoreMap":75},[79,455,456,459],{"class":81,"line":82},[79,457,458],{"class":93},"@",[79,460,461],{"class":85},"Bean\n",[79,463,464,466,468,470,472,474,477,480,482,485,487],{"class":81,"line":97},[79,465,33],{"class":85},[79,467,381],{"class":141},[79,469,113],{"class":93},[79,471,33],{"class":157},[79,473,161],{"class":93},[79,475,476],{"class":157},"Builder builder",[79,478,479],{"class":93},",",[79,481,90],{"class":85},[79,483,484],{"class":157}," dateTimeTools",[79,486,267],{"class":93},[79,488,94],{"class":93},[79,490,491,494],{"class":81,"line":104},[79,492,493],{"class":153},"    return",[79,495,496],{"class":157}," builder\n",[79,498,499,502,505,507,510],{"class":81,"line":135},[79,500,501],{"class":93},"            .",[79,503,504],{"class":141},"defaultTools",[79,506,113],{"class":93},[79,508,509],{"class":157},"dateTimeTools",[79,511,132],{"class":93},[79,513,514,516,519],{"class":81,"line":150},[79,515,501],{"class":93},[79,517,518],{"class":141},"build",[79,520,198],{"class":93},[79,522,523],{"class":81,"line":201},[79,524,355],{"class":93},[15,526,527,528,530,531,534,535,538],{},"The ",[31,529,116],{}," fields are the only information the model uses to decide ",[19,532,533],{},"when"," and ",[19,536,537],{},"how"," to use a tool. Keep them short but precise, and state parameter formats.",[10,540,542],{"id":541},"_3-a-realistic-example-order-status","3. A realistic example: order status",[70,544,546],{"className":72,"code":545,"language":74,"meta":75,"style":75},"record OrderStatus(String orderNumber, String status, LocalDate estimatedDelivery) { }\n\n@Component\nclass OrderTools {\n\n    private final OrderRepository orders;\n\n    OrderTools(OrderRepository orders) {\n        this.orders = orders;\n    }\n\n    @Tool(description = \"Returns the status and estimated delivery date of an order by its order number\")\n    OrderStatus getOrderStatus(\n            @ToolParam(description = \"Order number, e.g. ORD-12345\") String orderNumber,\n            ToolContext toolContext) {\n\n        var customerId = (Long) toolContext.getContext().get(\"customerId\");\n\n        return orders.findByOrderNumberAndCustomerId(orderNumber, customerId)\n                .map(o -> new OrderStatus(o.getOrderNumber(), o.getStatus().name(), o.getEstimatedDelivery()))\n                .orElseThrow(() -> new IllegalArgumentException(\"Order not found: \" + orderNumber));\n    }\n}\n",[31,547,548,585,589,596,605,609,626,630,646,660,664,668,689,699,726,739,744,787,792,816,876,912,917],{"__ignoreMap":75},[79,549,550,553,556,558,560,563,565,567,570,572,574,577,579,582],{"class":81,"line":82},[79,551,552],{"class":85},"record",[79,554,555],{"class":89}," OrderStatus",[79,557,113],{"class":93},[79,559,372],{"class":85},[79,561,562],{"class":157}," orderNumber",[79,564,479],{"class":93},[79,566,270],{"class":85},[79,568,569],{"class":157}," status",[79,571,479],{"class":93},[79,573,317],{"class":85},[79,575,576],{"class":157}," estimatedDelivery",[79,578,267],{"class":93},[79,580,581],{"class":93}," {",[79,583,584],{"class":93}," }\n",[79,586,587],{"class":81,"line":97},[79,588,101],{"emptyLinePlaceholder":100},[79,590,591,593],{"class":81,"line":104},[79,592,458],{"class":93},[79,594,595],{"class":85},"Component\n",[79,597,598,600,603],{"class":81,"line":135},[79,599,86],{"class":85},[79,601,602],{"class":89}," OrderTools",[79,604,94],{"class":93},[79,606,607],{"class":81,"line":150},[79,608,101],{"emptyLinePlaceholder":100},[79,610,611,614,617,620,623],{"class":81,"line":201},[79,612,613],{"class":85},"    private",[79,615,616],{"class":85}," final",[79,618,619],{"class":85}," OrderRepository",[79,621,622],{"class":157}," orders",[79,624,625],{"class":93},";\n",[79,627,628],{"class":81,"line":207},[79,629,101],{"emptyLinePlaceholder":100},[79,631,632,635,637,640,642,644],{"class":81,"line":212},[79,633,634],{"class":141},"    OrderTools",[79,636,113],{"class":93},[79,638,639],{"class":85},"OrderRepository",[79,641,622],{"class":273},[79,643,267],{"class":93},[79,645,94],{"class":93},[79,647,648,651,654,656,658],{"class":81,"line":234},[79,649,650],{"class":93},"        this.",[79,652,653],{"class":157},"orders ",[79,655,378],{"class":93},[79,657,622],{"class":157},[79,659,625],{"class":93},[79,661,662],{"class":81,"line":245},[79,663,204],{"class":93},[79,665,666],{"class":81,"line":280},[79,667,101],{"emptyLinePlaceholder":100},[79,669,670,672,674,676,678,680,682,685,687],{"class":81,"line":312},[79,671,107],{"class":93},[79,673,110],{"class":85},[79,675,113],{"class":93},[79,677,116],{"class":89},[79,679,119],{"class":93},[79,681,122],{"class":93},[79,683,684],{"class":125},"Returns the status and estimated delivery date of an order by its order number",[79,686,129],{"class":93},[79,688,132],{"class":93},[79,690,691,694,697],{"class":81,"line":347},[79,692,693],{"class":85},"    OrderStatus",[79,695,696],{"class":141}," getOrderStatus",[79,698,242],{"class":93},[79,700,701,703,705,707,709,711,713,716,718,720,722,724],{"class":81,"line":352},[79,702,248],{"class":93},[79,704,251],{"class":85},[79,706,113],{"class":93},[79,708,116],{"class":89},[79,710,119],{"class":93},[79,712,122],{"class":93},[79,714,715],{"class":125},"Order number, e.g. ORD-12345",[79,717,129],{"class":93},[79,719,267],{"class":93},[79,721,270],{"class":85},[79,723,562],{"class":273},[79,725,277],{"class":93},[79,727,729,732,735,737],{"class":81,"line":728},15,[79,730,731],{"class":85},"            ToolContext",[79,733,734],{"class":273}," toolContext",[79,736,267],{"class":93},[79,738,94],{"class":93},[79,740,742],{"class":81,"line":741},16,[79,743,101],{"emptyLinePlaceholder":100},[79,745,747,750,753,755,758,761,763,765,767,770,772,775,777,779,782,784],{"class":81,"line":746},17,[79,748,749],{"class":85},"        var",[79,751,752],{"class":157}," customerId ",[79,754,378],{"class":93},[79,756,757],{"class":93}," (",[79,759,760],{"class":157},"Long",[79,762,267],{"class":93},[79,764,734],{"class":157},[79,766,161],{"class":93},[79,768,769],{"class":141},"getContext",[79,771,167],{"class":93},[79,773,774],{"class":141},"get",[79,776,113],{"class":93},[79,778,129],{"class":93},[79,780,781],{"class":125},"customerId",[79,783,129],{"class":93},[79,785,786],{"class":93},");\n",[79,788,790],{"class":81,"line":789},18,[79,791,101],{"emptyLinePlaceholder":100},[79,793,795,797,799,801,804,806,809,811,814],{"class":81,"line":794},19,[79,796,154],{"class":153},[79,798,622],{"class":157},[79,800,161],{"class":93},[79,802,803],{"class":141},"findByOrderNumberAndCustomerId",[79,805,113],{"class":93},[79,807,808],{"class":157},"orderNumber",[79,810,479],{"class":93},[79,812,813],{"class":157}," customerId",[79,815,132],{"class":93},[79,817,819,822,825,827,830,833,836,838,840,843,845,848,851,854,856,859,861,864,866,868,870,873],{"class":81,"line":818},20,[79,820,821],{"class":93},"                .",[79,823,824],{"class":141},"map",[79,826,113],{"class":93},[79,828,829],{"class":157},"o ",[79,831,832],{"class":85},"->",[79,834,835],{"class":153}," new",[79,837,555],{"class":141},[79,839,113],{"class":93},[79,841,842],{"class":157},"o",[79,844,161],{"class":93},[79,846,847],{"class":141},"getOrderNumber",[79,849,850],{"class":93},"(),",[79,852,853],{"class":157}," o",[79,855,161],{"class":93},[79,857,858],{"class":141},"getStatus",[79,860,167],{"class":93},[79,862,863],{"class":141},"name",[79,865,850],{"class":93},[79,867,853],{"class":157},[79,869,161],{"class":93},[79,871,872],{"class":141},"getEstimatedDelivery",[79,874,875],{"class":93},"()))\n",[79,877,879,881,884,887,890,892,895,897,899,902,904,907,909],{"class":81,"line":878},21,[79,880,821],{"class":93},[79,882,883],{"class":141},"orElseThrow",[79,885,886],{"class":93},"(()",[79,888,889],{"class":85}," ->",[79,891,835],{"class":153},[79,893,894],{"class":141}," IllegalArgumentException",[79,896,113],{"class":93},[79,898,129],{"class":93},[79,900,901],{"class":125},"Order not found: ",[79,903,129],{"class":93},[79,905,906],{"class":93}," +",[79,908,562],{"class":157},[79,910,911],{"class":93},"));\n",[79,913,915],{"class":81,"line":914},22,[79,916,204],{"class":93},[79,918,920],{"class":81,"line":919},23,[79,921,355],{"class":93},[15,923,924],{},"When a tool returns a record, Spring AI serializes the result to JSON and sends it to the model.",[10,926,928],{"id":927},"_4-toolcontext-context-the-model-never-sees","4. ToolContext: context the model never sees",[15,930,527,931,933,934,937,938,941],{},[31,932,781],{}," above doesn't come from the model. Values like the user id or tenant must ",[19,935,936],{},"never"," be parameters the model fills in; otherwise a user could ask for another customer's data through the prompt. These values are supplied by the application via ",[31,939,940],{},"ToolContext"," and are not sent to the model:",[70,943,945],{"className":72,"code":944,"language":74,"meta":75,"style":75},"String answer = chatClient.prompt()\n        .user(\"Where is my order ORD-12345?\")\n        .tools(orderTools)\n        .toolContext(Map.of(\"customerId\", currentUser.id()))\n        .call()\n        .content();\n",[31,946,947,963,980,993,1029,1037],{"__ignoreMap":75},[79,948,949,951,953,955,957,959,961],{"class":81,"line":82},[79,950,372],{"class":85},[79,952,375],{"class":157},[79,954,378],{"class":93},[79,956,381],{"class":157},[79,958,161],{"class":93},[79,960,386],{"class":141},[79,962,389],{"class":93},[79,964,965,967,969,971,973,976,978],{"class":81,"line":97},[79,966,394],{"class":93},[79,968,397],{"class":141},[79,970,113],{"class":93},[79,972,129],{"class":93},[79,974,975],{"class":125},"Where is my order ORD-12345?",[79,977,129],{"class":93},[79,979,132],{"class":93},[79,981,982,984,986,988,991],{"class":81,"line":104},[79,983,394],{"class":93},[79,985,415],{"class":141},[79,987,113],{"class":93},[79,989,990],{"class":157},"orderTools",[79,992,132],{"class":93},[79,994,995,997,1000,1002,1005,1007,1009,1011,1013,1015,1017,1019,1022,1024,1027],{"class":81,"line":135},[79,996,394],{"class":93},[79,998,999],{"class":141},"toolContext",[79,1001,113],{"class":93},[79,1003,1004],{"class":157},"Map",[79,1006,161],{"class":93},[79,1008,180],{"class":141},[79,1010,113],{"class":93},[79,1012,129],{"class":93},[79,1014,781],{"class":125},[79,1016,129],{"class":93},[79,1018,479],{"class":93},[79,1020,1021],{"class":157}," currentUser",[79,1023,161],{"class":93},[79,1025,1026],{"class":141},"id",[79,1028,875],{"class":93},[79,1030,1031,1033,1035],{"class":81,"line":150},[79,1032,394],{"class":93},[79,1034,432],{"class":141},[79,1036,389],{"class":93},[79,1038,1039,1041,1043],{"class":81,"line":201},[79,1040,394],{"class":93},[79,1042,441],{"class":141},[79,1044,198],{"class":93},[10,1046,1048],{"id":1047},"_5-returndirect","5. returnDirect",[15,1050,1051],{},"Sometimes the tool's result already is the final answer for the user and doesn't need to go back through the model:",[70,1053,1055],{"className":72,"code":1054,"language":74,"meta":75,"style":75},"@Tool(description = \"Returns the download link for the order's invoice PDF\", returnDirect = true)\nString invoiceLink(@ToolParam(description = \"Order number\") String orderNumber) {\n    return \"https:\u002F\u002Fexample.com\u002Finvoices\u002F\" + orderNumber + \".pdf\";\n}\n",[31,1056,1057,1086,1121,1149],{"__ignoreMap":75},[79,1058,1059,1061,1063,1065,1067,1069,1071,1074,1076,1078,1081,1083],{"class":81,"line":82},[79,1060,458],{"class":93},[79,1062,110],{"class":85},[79,1064,113],{"class":93},[79,1066,116],{"class":89},[79,1068,119],{"class":93},[79,1070,122],{"class":93},[79,1072,1073],{"class":125},"Returns the download link for the order's invoice PDF",[79,1075,129],{"class":93},[79,1077,479],{"class":93},[79,1079,1080],{"class":89}," returnDirect",[79,1082,119],{"class":93},[79,1084,1085],{"class":93}," true)\n",[79,1087,1088,1090,1093,1096,1098,1100,1102,1104,1106,1109,1111,1113,1115,1117,1119],{"class":81,"line":97},[79,1089,372],{"class":85},[79,1091,1092],{"class":141}," invoiceLink",[79,1094,1095],{"class":93},"(@",[79,1097,251],{"class":85},[79,1099,113],{"class":93},[79,1101,116],{"class":89},[79,1103,119],{"class":93},[79,1105,122],{"class":93},[79,1107,1108],{"class":125},"Order number",[79,1110,129],{"class":93},[79,1112,267],{"class":93},[79,1114,270],{"class":85},[79,1116,562],{"class":157},[79,1118,267],{"class":93},[79,1120,94],{"class":93},[79,1122,1123,1125,1127,1130,1132,1134,1137,1140,1142,1145,1147],{"class":81,"line":104},[79,1124,493],{"class":153},[79,1126,122],{"class":93},[79,1128,1129],{"class":125},"https:\u002F\u002Fexample.com\u002Finvoices\u002F",[79,1131,129],{"class":93},[79,1133,906],{"class":93},[79,1135,1136],{"class":157}," orderNumber ",[79,1138,1139],{"class":93},"+",[79,1141,122],{"class":93},[79,1143,1144],{"class":125},".pdf",[79,1146,129],{"class":93},[79,1148,625],{"class":93},[79,1150,1151],{"class":81,"line":135},[79,1152,355],{"class":93},[15,1154,1155,1156,1159],{},"With ",[31,1157,1158],{},"returnDirect = true"," the result goes straight back to the caller; there is no extra model call and no extra token cost.",[10,1161,1163],{"id":1162},"_6-error-handling-and-security","6. Error handling and security",[15,1165,1166],{},"Tools run with your application's permissions. However good the model is, treat its arguments as untrusted user input.",[1168,1169,1170,1183],"table",{},[1171,1172,1173],"thead",{},[1174,1175,1176,1180],"tr",{},[1177,1178,1179],"th",{},"Rule",[1177,1181,1182],{},"Why",[1184,1185,1186,1195,1205,1213,1221,1229],"tbody",{},[1174,1187,1188,1192],{},[1189,1190,1191],"td",{},"Validate arguments",[1189,1193,1194],{},"The model may produce values in the wrong format or malicious ones",[1174,1196,1197,1202],{},[1189,1198,1199,1200],{},"Pass identity\u002Fauthorization via ",[31,1201,940],{},[1189,1203,1204],{},"Prevents access to another user's data through prompt injection",[1174,1206,1207,1210],{},[1189,1208,1209],{},"Separate read and write tools",[1189,1211,1212],{},"Lets you authorize risky operations separately",[1174,1214,1215,1218],{},[1189,1216,1217],{},"Ask the user to confirm mutating operations",[1189,1219,1220],{},"The model shouldn't cancel orders or make payments on its own",[1174,1222,1223,1226],{},[1189,1224,1225],{},"Return meaningful error messages",[1189,1227,1228],{},"The model can relay \"not found\" properly to the user",[1174,1230,1231,1234],{},[1189,1232,1233],{},"Keep the number of tools small",[1189,1235,1236],{},"Many tools increase the chance the model picks the wrong one",[15,1238,1239],{},"When a tool throws an exception, Spring AI by default sends the error message back to the model, which can then explain it to the user. So don't leak messages that contain sensitive internals (SQL, stack traces).",[10,1241,1243],{"id":1242},"_7-mcp-model-context-protocol","7. MCP: Model Context Protocol",[15,1245,1246],{},"MCP is an open protocol that lets AI applications access tools, resources and prompts in a standard way:",[1248,1249,1250,1256,1262],"ul",{},[50,1251,1252,1255],{},[19,1253,1254],{},"MCP server",": Exposes tools (and resources). E.g. your order service.",[50,1257,1258,1261],{},[19,1259,1260],{},"MCP client",": Discovers those tools and offers them to its LLM. E.g. Claude Desktop, an IDE, or your own Spring AI app.",[50,1263,1264,1267,1268,1271],{},[19,1265,1266],{},"Transport",": ",[31,1269,1270],{},"stdio"," for local processes, an HTTP-based transport for remote servers.",[15,1273,1274,1275,1278],{},"The payoff: once you publish ",[31,1276,1277],{},"OrderTools"," as an MCP server, both your own applications and any MCP-capable client can use the same tools without code changes.",[10,1280,1282],{"id":1281},"_8-an-mcp-server-with-spring-ai","8. An MCP server with Spring AI",[70,1284,1288],{"className":1285,"code":1286,"language":1287,"meta":75,"style":75},"language-xml shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u003Cdependency>\n    \u003CgroupId>org.springframework.ai\u003C\u002FgroupId>\n    \u003CartifactId>spring-ai-starter-mcp-server-webmvc\u003C\u002FartifactId>\n\u003C\u002Fdependency>\n","xml",[31,1289,1290,1302,1323,1341],{"__ignoreMap":75},[79,1291,1292,1295,1299],{"class":81,"line":82},[79,1293,1294],{"class":93},"\u003C",[79,1296,1298],{"class":1297},"swJcz","dependency",[79,1300,1301],{"class":93},">\n",[79,1303,1304,1307,1310,1313,1316,1319,1321],{"class":81,"line":97},[79,1305,1306],{"class":93},"    \u003C",[79,1308,1309],{"class":1297},"groupId",[79,1311,1312],{"class":93},">",[79,1314,1315],{"class":157},"org.springframework.ai",[79,1317,1318],{"class":93},"\u003C\u002F",[79,1320,1309],{"class":1297},[79,1322,1301],{"class":93},[79,1324,1325,1327,1330,1332,1335,1337,1339],{"class":81,"line":104},[79,1326,1306],{"class":93},[79,1328,1329],{"class":1297},"artifactId",[79,1331,1312],{"class":93},[79,1333,1334],{"class":157},"spring-ai-starter-mcp-server-webmvc",[79,1336,1318],{"class":93},[79,1338,1329],{"class":1297},[79,1340,1301],{"class":93},[79,1342,1343,1345,1347],{"class":81,"line":135},[79,1344,1318],{"class":93},[79,1346,1298],{"class":1297},[79,1348,1301],{"class":93},[15,1350,1351,1352,1355,1356,1359],{},"Publish your ",[31,1353,1354],{},"@Tool"," methods with a ",[31,1357,1358],{},"ToolCallbackProvider"," bean:",[70,1361,1363],{"className":72,"code":1362,"language":74,"meta":75,"style":75},"@Configuration\nclass McpServerConfig {\n\n    @Bean\n    ToolCallbackProvider orderToolProvider(OrderTools orderTools) {\n        return MethodToolCallbackProvider.builder()\n                .toolObjects(orderTools)\n                .build();\n    }\n}\n",[31,1364,1365,1372,1381,1385,1391,1410,1424,1437,1445,1449],{"__ignoreMap":75},[79,1366,1367,1369],{"class":81,"line":82},[79,1368,458],{"class":93},[79,1370,1371],{"class":85},"Configuration\n",[79,1373,1374,1376,1379],{"class":81,"line":97},[79,1375,86],{"class":85},[79,1377,1378],{"class":89}," McpServerConfig",[79,1380,94],{"class":93},[79,1382,1383],{"class":81,"line":104},[79,1384,101],{"emptyLinePlaceholder":100},[79,1386,1387,1389],{"class":81,"line":135},[79,1388,107],{"class":93},[79,1390,461],{"class":85},[79,1392,1393,1396,1399,1401,1403,1406,1408],{"class":81,"line":150},[79,1394,1395],{"class":85},"    ToolCallbackProvider",[79,1397,1398],{"class":141}," orderToolProvider",[79,1400,113],{"class":93},[79,1402,1277],{"class":85},[79,1404,1405],{"class":273}," orderTools",[79,1407,267],{"class":93},[79,1409,94],{"class":93},[79,1411,1412,1414,1417,1419,1422],{"class":81,"line":201},[79,1413,154],{"class":153},[79,1415,1416],{"class":157}," MethodToolCallbackProvider",[79,1418,161],{"class":93},[79,1420,1421],{"class":141},"builder",[79,1423,389],{"class":93},[79,1425,1426,1428,1431,1433,1435],{"class":81,"line":207},[79,1427,821],{"class":93},[79,1429,1430],{"class":141},"toolObjects",[79,1432,113],{"class":93},[79,1434,990],{"class":157},[79,1436,132],{"class":93},[79,1438,1439,1441,1443],{"class":81,"line":212},[79,1440,821],{"class":93},[79,1442,518],{"class":141},[79,1444,198],{"class":93},[79,1446,1447],{"class":81,"line":234},[79,1448,204],{"class":93},[79,1450,1451],{"class":81,"line":245},[79,1452,355],{"class":93},[70,1454,1458],{"className":1455,"code":1456,"language":1457,"meta":75,"style":75},"language-properties shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","spring.ai.mcp.server.name=order-service\nspring.ai.mcp.server.version=1.0.0\n","properties",[31,1459,1460,1470],{"__ignoreMap":75},[79,1461,1462,1465,1467],{"class":81,"line":82},[79,1463,1464],{"class":1297},"spring.ai.mcp.server.name",[79,1466,378],{"class":93},[79,1468,1469],{"class":157},"order-service\n",[79,1471,1472,1475,1477],{"class":81,"line":97},[79,1473,1474],{"class":1297},"spring.ai.mcp.server.version",[79,1476,378],{"class":93},[79,1478,1479],{"class":157},"1.0.0\n",[15,1481,1482,1483,1486],{},"Once the application starts, the ",[31,1484,1485],{},"getOrderStatus"," tool is reachable over MCP. If you expose a remote MCP server to the internet, don't forget to add authentication with Spring Security. You'll also need to design how user context travels with MCP calls (e.g. read it from a token).",[10,1488,1490],{"id":1489},"_9-an-mcp-client-with-spring-ai","9. An MCP client with Spring AI",[70,1492,1494],{"className":1285,"code":1493,"language":1287,"meta":75,"style":75},"\u003Cdependency>\n    \u003CgroupId>org.springframework.ai\u003C\u002FgroupId>\n    \u003CartifactId>spring-ai-starter-mcp-client\u003C\u002FartifactId>\n\u003C\u002Fdependency>\n",[31,1495,1496,1504,1520,1537],{"__ignoreMap":75},[79,1497,1498,1500,1502],{"class":81,"line":82},[79,1499,1294],{"class":93},[79,1501,1298],{"class":1297},[79,1503,1301],{"class":93},[79,1505,1506,1508,1510,1512,1514,1516,1518],{"class":81,"line":97},[79,1507,1306],{"class":93},[79,1509,1309],{"class":1297},[79,1511,1312],{"class":93},[79,1513,1315],{"class":157},[79,1515,1318],{"class":93},[79,1517,1309],{"class":1297},[79,1519,1301],{"class":93},[79,1521,1522,1524,1526,1528,1531,1533,1535],{"class":81,"line":104},[79,1523,1306],{"class":93},[79,1525,1329],{"class":1297},[79,1527,1312],{"class":93},[79,1529,1530],{"class":157},"spring-ai-starter-mcp-client",[79,1532,1318],{"class":93},[79,1534,1329],{"class":1297},[79,1536,1301],{"class":93},[79,1538,1539,1541,1543],{"class":81,"line":135},[79,1540,1318],{"class":93},[79,1542,1298],{"class":1297},[79,1544,1301],{"class":93},[15,1546,1547,1548,362],{},"To start a local MCP server over ",[31,1549,1270],{},[70,1551,1553],{"className":1455,"code":1552,"language":1457,"meta":75,"style":75},"spring.ai.mcp.client.stdio.connections.filesystem.command=npx\nspring.ai.mcp.client.stdio.connections.filesystem.args=-y,@modelcontextprotocol\u002Fserver-filesystem,\u002Ftmp\u002Fdocs\n",[31,1554,1555,1565],{"__ignoreMap":75},[79,1556,1557,1560,1562],{"class":81,"line":82},[79,1558,1559],{"class":1297},"spring.ai.mcp.client.stdio.connections.filesystem.command",[79,1561,378],{"class":93},[79,1563,1564],{"class":157},"npx\n",[79,1566,1567,1570,1572],{"class":81,"line":97},[79,1568,1569],{"class":1297},"spring.ai.mcp.client.stdio.connections.filesystem.args",[79,1571,378],{"class":93},[79,1573,1574],{"class":157},"-y,@modelcontextprotocol\u002Fserver-filesystem,\u002Ftmp\u002Fdocs\n",[15,1576,1577,1578,1581],{},"Remote (HTTP) servers are also configured under ",[31,1579,1580],{},"spring.ai.mcp.client","; since the supported transports and property names change between versions, check the documentation for the Spring AI version you use.",[15,1583,1584,1585,1587,1588,362],{},"The client starter exposes the tools of connected servers as a ",[31,1586,1358],{},". All you need is to hand it to ",[31,1589,33],{},[70,1591,1593],{"className":72,"code":1592,"language":74,"meta":75,"style":75},"@Bean\nChatClient chatClient(ChatClient.Builder builder, ToolCallbackProvider mcpTools) {\n    return builder\n            .defaultToolCallbacks(mcpTools)\n            .build();\n}\n",[31,1594,1595,1601,1627,1633,1647,1655],{"__ignoreMap":75},[79,1596,1597,1599],{"class":81,"line":82},[79,1598,458],{"class":93},[79,1600,461],{"class":85},[79,1602,1603,1605,1607,1609,1611,1613,1615,1617,1620,1623,1625],{"class":81,"line":97},[79,1604,33],{"class":85},[79,1606,381],{"class":141},[79,1608,113],{"class":93},[79,1610,33],{"class":157},[79,1612,161],{"class":93},[79,1614,476],{"class":157},[79,1616,479],{"class":93},[79,1618,1619],{"class":85}," ToolCallbackProvider",[79,1621,1622],{"class":157}," mcpTools",[79,1624,267],{"class":93},[79,1626,94],{"class":93},[79,1628,1629,1631],{"class":81,"line":104},[79,1630,493],{"class":153},[79,1632,496],{"class":157},[79,1634,1635,1637,1640,1642,1645],{"class":81,"line":135},[79,1636,501],{"class":93},[79,1638,1639],{"class":141},"defaultToolCallbacks",[79,1641,113],{"class":93},[79,1643,1644],{"class":157},"mcpTools",[79,1646,132],{"class":93},[79,1648,1649,1651,1653],{"class":81,"line":150},[79,1650,501],{"class":93},[79,1652,518],{"class":141},[79,1654,198],{"class":93},[79,1656,1657],{"class":81,"line":201},[79,1658,355],{"class":93},[15,1660,1661,1662,1664],{},"Now the model can use the tools offered by MCP servers just like local ",[31,1663,1354],{}," methods.",[10,1666,1668],{"id":1667},"_10-a-note-on-versions","10. A note on versions",[15,1670,1671],{},"The MCP specification and Spring AI's MCP support are evolving quickly. Annotation-based MCP APIs, transport options and property names can change between versions. Use the Spring AI line that matches your Spring Boot version (1.x for Boot 3.4\u002F3.5, 2.x for Boot 4) and compare the examples with that version's reference documentation.",[10,1673,1675],{"id":1674},"conclusion","Conclusion",[15,1677,1678,1679,1681,1682,1684,1685,1687],{},"Tool calling turns an LLM from a model that only generates text into an assistant that works with your application's data. In Spring AI, adding ",[31,1680,1354],{}," to a method and handing it to ",[31,1683,33],{}," is enough, and ",[31,1686,940],{}," keeps the security context separate from the model. With MCP you write the same tools once and open them to every AI client. The next step is letting the model answer from your own documents: RAG and vector databases.",[1689,1690,1691],"style",{},"html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}",{"title":75,"searchDepth":97,"depth":97,"links":1693},[1694,1695,1696,1697,1698,1699,1700,1701,1702,1703,1704,1705],{"id":12,"depth":97,"text":13},{"id":37,"depth":97,"text":38},{"id":67,"depth":97,"text":68},{"id":541,"depth":97,"text":542},{"id":927,"depth":97,"text":928},{"id":1047,"depth":97,"text":1048},{"id":1162,"depth":97,"text":1163},{"id":1242,"depth":97,"text":1243},{"id":1281,"depth":97,"text":1282},{"id":1489,"depth":97,"text":1490},{"id":1667,"depth":97,"text":1668},{"id":1674,"depth":97,"text":1675},"2026-11-24","Exposing your application's functions to an LLM with Spring AI — tool calling with @Tool, ToolContext, returnDirect, security, and setting up a Model Context Protocol (MCP) server and client.","md",{},"\u002Fen\u002Fblog\u002Fspring-ai-tool-calling-mcp",{"title":5,"description":1707},"en\u002Fblog\u002Fspring-ai-tool-calling-mcp",[74,1714,1715,1716,1717],"spring","spring-ai","ai","guide","\u002Fblog\u002Fspring-ai-tool-calling-mcp","5T06peWnCINgjXcypSXHbVpqsOBFy8STz5Od0Ftfhbs",[1721,1727,1735],{"path":1722,"title":1723,"description":1724,"date":1725,"tags":1726},"\u002Fen\u002Fblog\u002Fspring-ai-getting-started","Getting Started with Spring AI: ChatClient, Prompts, Structured Output and Chat Memory","Adding LLM support to a Spring Boot app with Spring AI — setup, the ChatClient API, prompt templates, structured output into records, streaming and conversation memory.","2026-11-23",[74,1714,1715,1716,1717],{"path":1728,"title":1729,"description":1730,"date":1731,"tags":1732},"\u002Fen\u002Fblog\u002Fspring-transactional-and-locking","@Transactional Pitfalls and Locking with Spring and Hibernate","How @Transactional works through proxies, self-invocation, rollback rules, propagation, readOnly, optimistic and pessimistic locking, and the SKIP LOCKED job-queue pattern.","2026-11-22",[74,1714,1733,1734,1717],"hibernate","database",{"path":1736,"title":1737,"description":1738,"date":1739,"tags":1740},"\u002Fen\u002Fblog\u002Fhibernate-n-plus-one-problem","Solving the N+1 Query Problem in Hibernate and Spring Data JPA","How to detect the N+1 query problem and fix it with JOIN FETCH, @EntityGraph, @BatchSize, SUBSELECT and DTO projections — a practical guide with examples.","2026-11-21",[74,1733,1741,1734,1717],"jpa",1795497414180]