Video: Enhancing your React App with AI without Rewriting it | Duration: 5268s | Summary: Enhancing your React App with AI without Rewriting it | Chapters: Introduction and Setup (19.135s), Workshop Overview and Setup (88.01s), Cloudflare Deployment Overview (333.295s), Workshop Overview Continued (427.15s), Setting Up Project (521.085s), AI Search Integration (864.355s), Tool Calling Theory (1830.935s), Synthetic Tool Calling (2105.89s), AI Implementation Overview (2562.835s), AI Search Implementation (2773.075s), Client-Server AI Integration (3623.575s), Prompt Adjustment Strategy (3686.92s), Configuring API Tools (3776.45s), Search Filter Properties (3854.705s), AI Search Implementation (3964.08s), AI Intermediary Step (4061.975s), Model Context Protocol (4166.78s), MCP Implementation Basics (4292.075s), Durable Objects Introduction (4410.46s), MCP Server Integration (4513.95s), MCP Capabilities Overview (4628.16s)
Transcript for "Enhancing your React App with AI without Rewriting it":
Hello. Hello, everybody. We'll just give it a couple of minutes, and then, we'll get started. But if you want to, get going, this is gonna be an interactive workshop. Thank you for joining me. If you just wanna get started by taking a look at this GitHub repository, you clone it down. We've got a couple of lessons that we'll go through. So the purpose of today is that you go away with, some sort of practical patterns for how you can integrate AI into an existing app. I've chosen React because it's what I'm most comfortable with, but they should stand for any sort of app that you're building, be it web with any framework or mobile or whatever. So, like I said, there's a couple of lessons that we'll go through just exploring a few different techniques and ideas. So just go ahead and download this repository to your own computer, and I'll give you some more information in just a couple of minutes once we've got some more people who joined. I think I can probably actually copy and paste this link into chat. Let's do that for people. Just give it another minute. Okay. Cool. So like I said, right at the very start, this is a interactive workshop. We are going to be looking at how AI, some of the older stuff, some of the newer stuff, how that all can fit into an existing application that you're already working in. So the example that we have today is that we have like a a rental agency, application. So we can see properties and details about them, search through them, that sort of thing. And, we're just gonna be sprinkling in AI in, like, pretty sensible ways. So, it's broken up into a few different lessons. We will work through them together. I'll give you a little bit of time to get through them, but we don't have tons of time today. So I'll quickly, pretty quickly follow-up with, my own implementation. And if you ever get stuck at anything, feel free to just dump it in the chat. We can, debug a little bit. Or if we completely grind to a halt, don't worry because each lesson acts as a snapshot building upon the last. So if you just jump forwards to the next lesson, you'll be back in a good state again, and you can just pick up from there. And then in your own time, you can go back and look and see where the the issues were between the two checkpoints there. So the repository, I've posted it in chat. So please go and take a look at that. If you just clone this down, I'm using a pretty modern version of node, and we're gonna be using React because it's a framework I'm comfortable with, and TypeScript. But there's nothing crazy happening here. Still relatively modern stuff, React server components and server functions, but you'll probably be totally okay if you have any sort of coding experience. The things that we're focusing on today, of course, is the AI pieces. So if you're more familiar with Svelte or Vue or whatever, then you're you're probably still gonna be completely okay. The other prerequisite is a Cloudflare account, which I assume, most people here have. But if you don't, go and sign up for one of those now. Everything that we're gonna be doing today in this workshop is covered under the workers free tier. So, no issues with needing a credit card or anything like that. You should be good to go and just sign up. And, this is just giving you a taste of of what is possible with the Cloudflare Developer Platform. So, I obviously have it cloned already, but give it a clone and then install the dependencies. So here is the exact same readme we were just looking at, but in, Versus Code. I have a little terminal down here, which I'm going to clear out because I was still just making sure everything was still working. Here we go. So, I say, there's a few lessons, and they're all sort of check points of each other. If you actually dig into one of them, you realize that, Hey, this looks like a real project. And they all look like that. So they're all kind of cloned checkpoints for each other and they're all running in different ports. So that's the one thing that I'd like to flag right at the very start is if we're working in lesson one, though the development server for that is gonna be available at eight thousand and one. If you're in lesson two, it's gonna be available at eight thousand and two. This is just to make sure that there's no conflicts or whatever, but you just need to be, cautious that when you're operating in a directory, you are doing so knowing which lesson you're in. And also when you're testing your browser, testing app in the browser that you're listening on the right port. Hopefully, that's pretty straightforward. I'll give you a little bit of tease of what each lesson will entail. So the first one is literally just taking the application that we have and deploying it to Cloudflare. We're gonna set up a database for it, and we're going to deploy this React app using workers assets. It's literally just to get the thing live and make sure that you're good with your Cloudflare account. The second lesson is going to be the biggest. It is about, taking some we're adding in some new components into our app, which are going to be calling our AI functions, and we're gonna be enhancing the search functionality that we have in our rental agency application. The third is going to be taking a look at MCP model context protocol, and we're gonna be implementing an MCP server for our business so that you're able to interact with our business from other clients, AI native clients, things like ChatGPT or Claude or whatever you're using. And then the last one is a sort of take home, choose your own adventure. It's just couple of ideas of different directions that you could take this project, things that we didn't cover in, the first couple of lessons, but are just natural extensions. So you can take a look at there for some ideas and have a shot and then let me know how it gets on, how you're going with it. So the very last thing I'll say is that we do have a Google form here at the end, which I will click the button for making that go live, just to collect some feedback from you guys just so that I, know how I'm doing and, can hopefully make improvements to this workshop and any of the future workshops that I run. So, give me a minute, and I will make that, live before the end. But if you do have a minute, please do fill fill that out and let me know how you found it. Okay. That is the brain dump at the start of this thing. If anyone has any questions, please just chuck them into chat. I will try and take a look and work through them. But hopefully you've got this cloned. You've got a Cloudflare account set up now and we are ready to dive into lesson one. So, the way I will run these is that we'll sort of introduce each lesson. I'll give you a couple of pointers, Let you try it out yourself. I will then follow-up with implementing it myself live on screen. Hopefully it goes relatively smoothly and then q and a, and then we move on to the next lesson. Happy days. So with all of that done, let's dig straight into the very first lesson. So like I say, this is a React application. It is using d one, our SQLite serverless database. And that's gonna be storing all of the information about the properties that we have for our hypothetical rental agency. I have some extensions that you can install if you're using Versus Code or Versus Code fork. And I've got some script set up to start with just to make sure that the project is working, that everything is being cloned and installed correctly. We create a d one database using this Wrangler command here. Wrangler is the Cloudflare CLI command line interface. It is the tool with which you interact with the developer platform. So this actually creates a real production database, that we will then use when we make the the deployment. And there's a little bit above the config that you need to do just to stitch up that brand new database to this app so we know which one to use. We have migrations, which I'll talk about in just a minute when we go through it live. We spin up the development server, we build it, we make sure it's working, all that sort of stuff. Then we deploy it. And like I say, very simple first lesson. All we're doing is just getting this thing deployed and it's in lessons two and three where we really actually lean into the AI pieces and, play around with that. So just, have a crack going through this first few setup steps, and I will follow-up in in five minutes myself, and we can do them live in case anyone got stuck. But, yeah, let me know if anyone's having any issues. And in fact, you know what might be handy for me is if you are done with the first lesson, if you just post in chat. Because otherwise, it's quite hard for me to judge the right amount of time. So, that would be really handy if I can get a sense just how fast people are going through this. Thank you. Someone's asking a question about registering a workers.dev subdomain. So, yeah, if you've never used, the Cloudflare developer platform before, when you just go into the Cloudflare dashboard, I think one is probably automatically created for you. If not, then yes, absolutely. Go ahead and pick a workers.dev sub domain. Workers.dev is a free URL that we give you when you deploy to Cloudflare. And, it's just a place that you can put these projects. You don't need to necessarily bring a, custom domain, although, of course, you can if you want to. Great for little prototype projects exactly like this. So, yeah, if you register for that now, it can take just a minute or two sometimes for, like, DNS and certificates and stuff to propagate. So get that going right now. And if you sort of reach the end of this and you deploy and it's still not quite available, just give it another few seconds and hopefully it it is available soon. Fun fact, we have a project in the works which hopefully speeds up some of that stuff, but, not quite ready just yet. Okay. I'm gonna start going, through it myself now, and, hopefully, people can keep going and, I'll just catch them up. So we're in the root at the moment, so I need to c d into lesson one. Like I say, this is very important to make sure you know where you're operating in because they all look very, very similar. So the very first step is just running NPM run check. This is running linting and testing and that sort of thing. I'm just making sure that the project is in a good state and hopefully this passes no problems. It does. Fantastic. And like I say, we're gonna create a d one database. So I'm just gonna copy and paste this command in here. What we are doing is we're creating a database called React rental agency. And we're saying, Hey, we want this available on the binding DB. We'll talk about bindings in just a minute once we've got this hooked up. But, for now, all we need to do is copy the database ID that was created for us And we pop that into our Wrangler TOML. I've left a very helpful comment here saying exactly where that should go. So we're just updating this line here. Now for this database that we have, the DB, bound database, we want to apply the migrations. If you have a take a look at what the migrations actually are, we'll see there's two of them here. There's an init one and a seed one. Now this is much more than we actually need for this project. But this is a hypothetical sort of database structure that we might have for our rental agency. We have like the contacts that we have, be it tenants or landlords. We have the list of properties. This is the one we're actually gonna be using. A bunch of different properties about the properties, things like the number of bedrooms that they have, the rent, etcetera, etcetera, and tenancies. We don't use that either. If we took a look at seeding, there we go. This is just a bunch of fake data I asked AI to generate for me, and, this just gives us a starting off point for application with some fake properties that we can use to filter through and all that sort of stuff. So, we need to actually apply these migrations, and this is applying it, remotely. So this is applying it to our production database. The, local one happens automatically for us when we start the development server. So we're being asked, do we wanna apply this thing? It might hold our database in prod, but that's fine. We're not using it yet. So apply the first, apply the second. We are good to go. So now we can spin up the local dev server, npm run dev. And this is, like I said, going to run the migrations locally for us. And then hopefully that proceeds. Yep. And we have Vite here spinning up a local copy of our application. So let's visit that. Oh, let's open it in the wrong tab. Just one moment. Here we go. And there we go. 8,001. I see some more comments. Let me just quickly check what they're saying. Quite a few comments. I keep getting an error when trying to create a subdomain, but I'll create another name. Yeah. Sorry about that. If you just, either give it a minute, if you're just trying to access that domain, it will hopefully be available in just a moment. But if you're having an issue with the name itself, maybe try and remove any special characters that are causing an issue or shortening it if it's a very, very long thing. If you are still having an issue, you don't need to deploy this thing live. It will still work. Just you can just do the local development server. We can do everything, pretty much apart from the very end, all through just the local development server. So don't worry about it too much. Hopefully, you can just follow-up with the production deployment later in your own time. Another comment. The sidebar doesn't scroll for me, so I can't see the bottom after zooming out a lot. If that's of my own app, that's my fault. That's terrible coding of mine. If that's the Cloudflare dashboard, you probably don't need anything there. So, hopefully, that's not an issue. Okay. Yeah. It's not playing nice. Local it is. Okay. Sorry about that. But yep. Fix my app. Oh, it's my app. Okay. Yep. Anyway, let me let me log into this myself. So here we go. You're right. This sidebar thing doesn't scroll, but it does if you get to the very, very bottom. Ta da, I'm an excellent cutter. Here we have 24 properties. These are the seeded properties that we had in that migration script that we saw. And they all have a bunch of different things and I can filter for vacant only two bedrooms. I can say I need in unit laundry and I can apply my filters. Look, I found the properties that are, relevant to my search. The way that this is implemented, let's just go and take a peek behind the curtain. If we go into the source folder pages, we have, property search. In here, we are creating this form, which has all of the searches being applied and where the search filters, and we have a map of all of the available properties. And like I say, this is using, certain components and all that fun stuff. The, property search itself is just a I forget what I'm even using here. Is this Drizzle? Yes. Drizzle is a ORM that operates really well with d one. And so we're just able to describe quite easily in TypeScript to the search filters that we want to apply based off of the UI controls that people are selecting. And then down here at the bottom somewhere, we're going to be saying, hey. For the database, run it with those search queries and returns up pretty straightforward. So nothing crazy here. The only sort of interesting things is the way that we're getting the database. That I have a couple of utilities here, GET EMF, GET request. These are some context utilities using async local storage, which help to propagate kind of like React context platform level variables down into your application without needing to prop drill. So there's a couple of them here in the context dot ts file that we, we're gonna be reusing get m in the future when we want to interact with AI. But just know that this is also how you're getting to your your database. Okay. So that's a brief overview of the application, how it works, and how it runs. And we're now just gonna build the thing really quickly. I'm gonna need time. Okay. I don't know. We're doing okay. Build it. Hopefully, that succeeds. Another thing you can do is run preview. Preview is using Wrangler rather than Vite to serve the application, and this is just an also slightly more realistic server. So if you're ever having an issue between prod, let's say prod is broken, but your local thing is working just fine. Running at preview or using the Wrangler dev server is the most accurate production emulation you'll get locally. And so that can just help, like, figure out if you're having issues, somewhere in the local to prod pipeline. We can help narrow down some some things, whether it's a VEET issue or whether it's a production issue. And assuming that's all working fine, I'm just gonna come out of that because I know it is. I'm gonna run Wrangler deploy, and this is going to take a look at the built application that we have. I'll just show you the structure of that in just a moment. Upload all of our static assets, upload our worker script, bind to our d one database, and make this available on our workers.dev URL. And like somebody says, I've already got one here, gregbrumboldt.workers.dev. And I just get a free one for this project, my React rental agency. Gregbrumboldt.workers.dev. So I promised the built structure. Here, we have two things. We have the client. We have the worker. The client is all of our static assets. These are our JavaScript, fonts, CSS styles, any images, all that sort of stuff. Classic front end static assets. And our worker is a lot more interesting. This is the executed on Cloudflare's network. And so if we go in here, this is a great big built oh, sorry, built file. So total garbage to us. But then at the bottom here, we'll find something that maybe looks a little bit familiar if you've ever deployed a worker before. Here, we're defining a fetch handler. So we're saying, hey, if you are answering HTTP requests, do so with this function. And so this is something that is serving our React server components, React server functions back end of our application, whereas the client is, of course, the front end. So together, they make our full stack app. Okay. That is all of the setup. And if I visit this URL, hopefully we have something working live. Okay. Great. So we can come in here. We can hopefully see all of our properties. Excellent. We can hopefully filter them in just the same way. Apply filters. Yes. I saw that number change. Great. We have 17 properties for this search. So hopefully everyone's at that point. If there's any other issues, please do just leave them in chat. But without further ado, I'm gonna move forward into lesson two. So lesson two, I warned you at the start. This is the longest one and it's the most that I leave you alone for. So if you're getting a little bit stuck, don't worry about it. I anticipate this being a bit of a step up from the first. This is where we're introducing AI. So we need to come out of lesson one, go into lesson two, remember that the courts change in case you've got any tabs still open. Woah. So lesson two. I'm gonna put this in a nice preview. That's much clearer to read. If we just run NPM run dev, this is picking up exactly where lesson one left off. But the very first thing we're going to want to do is going to add AI to your application and science magic, but really all we're doing is adding the AI capability to our application. And this is literally two lines in our Wrangler TOML. All we need to do is say, where do we want this AI thing to be made available to us? And just like we have a d one binding for where we say our database is available, here we're saying, of our d one databases, we need a binding on DB. Here, we're creating saying, hey, for our AI capability, we want a binding called AI. I'm not very imaginative with my naming. We're gonna run this command here. This just regenerates our TypeScript types, letting, our editor know that we have now got this new capability informing about all of the types of that AI capability. And you can take a look at workerconfiguration.deep.ts to see, how that's changed now that we've added this AI binding. We can pick an AI model. Now this I'm gonna do right here right now. This is our model catalog here on the Cloudflare developer documentation. We have a whole bunch of models. Have, I have no idea, maybe hundred, hundred plus models, tons and tons of them, all with a bunch of different capabilities. I'm sure that you know there's, multimodal AI models. There's AI models for speech, like audio, some for images, all this sort of stuff. We're gonna be interested in just text models, but particularly ones that have the ability to do something called function calling or tool calling. So on the left here, have a capability checkbox for exactly that. And we see we've got a few models here that support it. I chose Hermes two Pro Minstrel seven b, for no particular reason other than the fact that it stood out because it had a beta label it. But I'm sure all the others would probably work just the same. So feel free to choose whatever you want, but know that all of the instructions are using Hermes two. We also have I'll introduce it now, the AI playground. This is playground.ai.cleft.com. Let me actually post some links here for people following along in chat. So here was the documentation for our model catalog, And this is our AI playground. And this is just a live interactive free place where you can come and try out all of these different models. So there's the here's the Hermes two, you can interact with it just like, hello. Same as or whatever. It works just the same. Great to just get a feel for things, and it will be something that we come back to later on. The goal of this lesson is to interact to integrate some AI into that search property search, like, functionality that we've already seen. So I have some instructions here on how you might do that using React server components and server functions, but know that, there's no, like, correct answer here. I just have like a way of doing it. And because we have all these lesson checkpoints, if you're getting stuck with this, if React is not your native language, then feel free to have a peek at lesson three and steal some code from there or whatever, to make this thing work. Like I say, I'm going to be following through with it live in just a minute anyway, but if you want to, try and keep pace, you are more than free to do that. I have some hints how you might start to think about things like system prompts. In here, there's just some like, techniques that were better than others. I'm by no means an expert in any of this like prompt engineering stuff, but it's just some things I toyed around with and found that worked quite well. The idea is that we're going to build an AI search tool thing. So if we go back to our application here, we have all of these manual filters and there's six dozen of them. And wouldn't it be nice if we had up at the top here, just a text box that we could type what we're looking for and let AI figure out how to search for us. We don't want it to do like, needle in the haystack stuff. We don't wanna give the AI all the properties and then just ask it to filter these things because that's a very nondeterministic, hard to inspect process, although you can do it. It's very, context heavy and just not a great approach for if you had let's say we were looking through, like, logs of an application. We're building like an observability dashboard. You couldn't feed every single log item that you had. It would get completely bombarded. And so, all we want to do is transform some free text that we're typing in into the filter criteria that we already have in our app. We know that works. It's super structured, works really well, super inspectable with drizzle, all that sort of stuff. So we're gonna keep all of that. All we wanna do is just introduce this pre filter stage that helps us construct all of these filters on the left. So we're going to be defining a function tool or whatever they're called, which has all these same sort of properties and let the AI figure out, okay, given this free text, which checkboxes should I check? Which value should I input into these textboxes? That sort of thing. And I'll go into the theory in just a minute. Basically, plumb that into our applications UI, run it, build it, deploy it. And hopefully, we have some working AI search at the end of this thing. The theory. So where which tab is that? This one? This one. This is a little bit much, so let me just just catch your breath. We'll focus here on the left hand side. So this is a diagram for typical tool calling chain. So we have a user here on the left in the client land. And they provide a user prompt. And this box here is our AI system rather than just the AI itself. The AI itself is this little LLM over here. So with the AI system, this is providing the or so brothers, combining the user prompt and the system prompt together along with any definitions of tools. It provides those messages and tools to the LLM. The LLM can then return messages or tool calls back to the system. And in a typical process, the user might be asking something like, Hey, What's this is out of date a little bit. Like, what's the weather in New York? Sorry. Live editing. Just gonna steal this. Okay. So the system prompt in this case is, like, you are a nice assistant. You behave and you follow these rules. These are, like, standard sorts of constraints that you want to apply to your LLM and it's sort of tonality and the rules it follows, that sort of thing. You provide a user prompt, which is in this case, what's the weather like in New York? And you would also say like, hey, there is a tool, get weather, and these are the things that can take. That's this first message here. The response is then either a message or a tool call. And in this case, it's a tool call. We've the AI has really, like, realized that it doesn't know the weather in New York right now. And so it needs to call a get weather tool with the following parameters. The location is New York, and it's the value here for us is that it's managed to sort of realize that NY means New York. It's done that sort of transformation for us from raw into structured. Woah. And that's what we're gonna be taking advantage of in our application where we want to take in a raw text field input and get back those structured search filters that we want to apply. Then the you would typically have the system would perform this tool call, get back some sort of response, and give it back to the AI. That's this purple here. So we've run the tool call. Now we pass in all the other messages because LLMs are very forgetful. And we say, Hey, the result of this tool call was this other structured data. And then finally, the AI is gonna return either message or a tool call. And in this case, it's a message because it has all the information it needs. And it's saying, hey, the weather, in New York is warm. It's mild wind. It's doing all that sort of recontextualizing back given the sort of tonality instructions and the user prompt it was given. So, that's the sort of full typical flow, but we don't need this second half here. We don't need to actually create a real tool call because we're not having a AI conversation. We're not building a chatbot here. All we're doing is this first part where we want to turn raw input into structured data and get extract out the filter parameters. So we're gonna terminate it here at this red line. All we need to do is provide just get the tool call back from the the LLM. And then on the diagram here, it's this, this red X. So we've done the user prompt, the system prompt, the tool calls, they go to the LLM. We get back the messages or tool calls, and then we stop things. And that simplifies things. We don't need to implement this entire system all end to end. But it's just a bit weird. And I'm calling this like synthetic tool calling because there's no actual tool that we end up calling. We just provide the AI this fake function search for properties. And ultimately, we end up doing that ourselves outside the context of the LLM conversation. Hopefully, that makes a bit of sense. If not, at least the read me makes a bit of sense, but if anyone's got any questions, dump them in the chat. And, yeah, feel free to now make a start on lesson two. Let me know how you're getting on. Let me know if you have any problems. And if somebody rattles through it, just let me know when you're when you're done. I'll give you guys maybe ten, fifteen minutes or something to work your way through that because I know it's it's quite a lot. Okay. One question straight away. Where do we specify which model we have chosen? Good question. So let's take a look at this model catalog. If we click into Ermes, two, assuming you chose that or whatever one, whatever, click into it, you'll see some usage examples here. And so if we go into the worker one here, this is our fetch handler. We saw that when we're just looking at exactly how workers works. We're providing in a bunch of these messages. This looks very similar to that TL draw diagram that we're just looking at, right? Like the system message up at the front and then the user prompt, following it. And you can see that you can call it on env AI run with the name of the model and then those messages that you want to provide it. And so you can just run this directly in the worker. So let's go to the worker right at the top here. You could do something like this dot m dot a I dot run. But the challenge that we're trying to do here is we're trying to integrate it into our React application. And so I have some instructions in the read me. Go down a little bit. About how to do this using oh, sorry. I just stopped sharing. Here we go. Okay. Sorry. So if you click into the model from the model catalog, go down into the usage, you see worker. Here's the stuff that I was just talking about where you can call, the function. This is the JavaScript example of how to call this AI model. And I'm saying that, boy, these messages sure look similar to the diagram that we were just looking at wherever that's gone. Like, this is the system prompt. This is the user prompt just as this is the system prompt. This is the user prompt. You can call this thing by going on on the m object AI. That's the name of the binding. Run the name of the model, which I think is probably right at the top here. Yeah. So can you just copy and paste this from here. And then you pass in the set of messages that you want. This thing takes messages, but also the function calling ones not only takes messages, but they also take some down here. Tools, list of tools that we want the system to be able to use. And so that's how we're gonna be doing this synthetic tool calling. We're gonna define a search for properties tool and give it all of the parameters that it can call it with and we'll extract them out given the user message. And I was saying that you can call this by just directly going into your worker work and we're in worker index TS. I can do this dot m dot ai dot run and like call it this way that will work. But because we're trying to integrate it into our React application, I do have some instructions on how we could do this with a server function. And so I suggest that you define a or you start a new file AI search inside the React app. So you can go into sources and pages and in here. And we can use that same get m helpful utility here that we're using to get the database. We can use this also to get the AI capability from, from the platform inside our React application. Hopefully, that's enough hints on where to get started with. But like I said, I'm gonna follow this up in just a moment with my own implementation. So, feel free to just wait for that. Cool. Yes. That seems to have made sense. Right. I'll give you, like I say, five, ten minutes or so to take a stab at it, and I will follow-up myself in just a minute. Great. Somebody's getting some, use from looking ahead at the other lessons. So, yeah, if you're stuck or just unfamiliar with your actual TypeScript or whatever, feel free to take a look at the earlier lessons, and that's just an implementation. Again, it's not the perfect one or necessarily the correct one or whatever. You're free to do it however you want, but this is just a way to do it. Will you have access to the chat notes once this session is complete? I have no idea, I'm afraid. Okay. Somebody's saying you've got the video. Great. It's being recorded, But, chat is maybe ephemeral. If you are dropping, I'm going to go ahead and, be very cheeky and give you this feedback link right now because I managed to make that working whenever I do this. How do I ship this thing? Publish. There we go. To anyone with the link. Save. Publish. Copy link. Okay. Cool. Perfect. Yeah. If you wouldn't mind, click in on that, and it just takes one minute just to, let me know what you think so far. Anyone else that needs to drop any point? Yeah. Just feel free to leave your thoughts and feedback there. Thank you very much. I'm gonna pin all these things. There we go. Pin message. Glad you enjoyed it. Fantastic. Okay. I think I'll maybe do half of it, and then, I'll give people a little bit more time because like I say, I know it's a very big one. But this way, maybe you just help unstick anybody who is, struggling. So let me make sure to share again. Here we go. So I think all I'll do is just sort of get, AI working, and I'll let everyone else sort of fiddle around with prompting the thing and getting all the plumbing totally totally worked out. But let's just get to the point where we've got the front end speaking to the back end and we're calling some sort of AI to do something. So first things first is get the AI capability. We're gonna go into our wrangler dot toml and I'm just gonna dump in the AI binding here. This itself doesn't do anything very exciting, but at least gives the ability to call AI for our application. We are going to regenerate our types. If I have a look at Wrangler sorry, worker configuration, we see here what's changed is we've got a new, line here on line 10. The AI binding on the environment now has this type AI and this here we define like all the sets of models and all that sort of stuff. So you get your nice TypeScript autocomplete, and that should get picked up inside our application. Now, we've picked the model already. We're gonna go with Hermes two. We're gonna create a AI search UI. So let's go into property search, search filters, and have a look at how exactly this works. So we have a bit of react state stuff up at the top here, And the rest of it is just all of our form fields. So, this is the sidebar that we see in our application. I am going to start another form and rather than handle submit, we're going to handle AI submit. And you could do this a number of different ways. I'm just going with a new form, so that I can more easily sort of take it in two steps and say, okay. This is the AIPs, and this is the structured search that we already have. I'm gonna just copy and paste all of nice formatting that we've already got. I don't know what a field set is, but let's have it. And, you know, set. I guess we want, like, a okay. Maybe a heading. Let's go with that. Above as well. I'm gonna say AI search. Here, we want kind of like a text area. So I have one of them somewhere. There it is. And we want, like, the submit button. That's basically it. It's gonna be at the bottom. Cool. So, yeah, we got a nice, like, pending state and all that sort of stuff. I'll steal all of that. Okay. I think we probably want a titles listing that seems to be this legend thing. I'm gonna say search. And there's a placeholder of, like, what are you looking for? Something like that. And rather than this doing is pending or, know, is AI pending, same as above, we can't come up with some very witty. And I don't think I need a reset button. Okay. So now we have like the UI, I think, probably. But we just need to define this sort of state and function handling and that sort of thing. So I think again, I'm just gonna pretty much copy what we have up here. So we need is AI pending. Start AI transition, and, handle AI submit. And, this is doing some, like, state stuff. We don't need any of that at the moment, but we'll just sort of make sure this renders correctly. So let's go npm run dev. And hopefully, this boots up here. If I go to 8,002. Cool. We got an AI search. We got buttons, all that fun stuff. If I type in anything here, it's obviously not gonna do anything yet, but we at least have like the UI rendering. Let us extract out this data. So I think if I was to do something like e dot target dot value, maybe something like that, you can do new form data from that. Maybe just eat out target. I I forget how to do this, to be honest. Forms form data. What did I call this thing? Oh, yeah. I need to give it a name. Name equals let's go with the ever imaginative AI search. AI search. And hopefully, this is just like dumping out into the console what I'm searching for. Yes. It is. Fantastic. Okay. So now we need to figure out how to get this to the server. And so we're gonna use a, server function for that. So for those that are unfamiliar, server functions can be defined in a few different ways, but we're just gonna go over a very clean new file here, AI search. And we mark modules or functions with the use server pragma. This is just a way to tell the React compiler that, hey, this thing is a, file that needs to run on the server versus some of the other, like, client components here. And we get to just define a function. So we're just gonna say this thing is an async function. AI search is gonna take in a prompt, which is gonna be a string. And it's gonna return maybe a error message or a filters set of filters, whatever they look like. I have no idea what shape they have yet, but let's go with just an array of objects. And let's do the other, filters. No. Error. No. Okay. And return. For now, let's return an error. Bang. Okay. I've messed up some stuff here. What is all of this? It's gonna return a promise of it because it's an async thing. I wouldn't worry about that too much. We're about to do it, but let's go with get m dot a I. Okay. That will at least, like, satisfy it. Away. Forget about it. This is just lending. This should still work. So we have now the, prompt. I'm gonna say this is the prompt, is our AI search value from our text area. And we're gonna call the AI search function that we just created with said prompt. And, hopefully, we're gonna get back. I'm just gonna call it response for now. So I'm gonna make this thing async, and we're gonna just log the response. Now what's it yelling at me? I'm just gonna commit the sins of saying this thing is a string, and this is a HTML form element. And this is unexpected await on non promise, non denable. Whatever. Let's just see if this works. So I'm expecting to get back. Bang. Yay. Okay. Cool. So we now have a client making a function call to the server. And in fact, I can demonstrate it to the server by doing a console dot log here of whatever the prompt is, console dot log prompt. Rather than seeing it here on the client, I am client prompt. Oh, we're getting back to bang. If I go to the server terminal, we're seeing it over there. So that way we know cool. We've got these two sort of ends speaking to each other. We're bridging that full stack gap or chasm as I've seen it called. And, okay. My TypeScript is terrible, but, at least we have that communication happening. And AI, of course, needs to run somewhere. And and part of this workshop, I thought about maybe doing some client side AI, which you can do in things like Chrome these days. But, I figured just keep it pretty straightforward. We're gonna be calling it from the server. Okay. Somebody's saying, I built an AI prover project previously in Cloudflare and couldn't get the AI bindings working locally. What's the trick you need to get it working on NPM run dev? I'm literally just about to get there. So we're running NPM run dev right now. This is not yet on prod. But the way that our development server works is that it's local first, should we say, rather than local exclusive. And so when we're serving something like a React application, that's all happening locally. That's giving you things like HMR and all that fun stuff. But when you need to call one of these massive AI models, that phones out to our remote infrastructure. And so you're still able to take advantage of the features like AI even when you're developing locally. So hopefully, it should work at least in this workshop. If you had issues with it before, hopefully, no longer, but we'll see how we get on. Maybe, maybe I struggled with it too. So we have the communications on both sides. Fantastic. Now let's do the AI calling. So I kinda started drafting it over here, but we need to call, like, get our m. I'm just gonna pull that out into its own thing just so I get m. And now I get to do m dot ai. This is our generated types now working for us. We see run. We get to call it with the Hermes two, that one. And we get to pass in messages. So I'm just gonna start with the example we had in the catalog. Just gonna grab these things. Cool. And let's try and return that to the UI just so that we are no longer looking at console messages, and we can actually start to get something end to end here. So I know that the response here is going to be useless to me. It's not my search filters yet. I haven't done any of that work. So I'm just gonna return it as an error and figure out how to render some errors on my React application. So let's say this is our response and you need to await it because it's an async function. And what we get back here is something. I'm not actually sure what it looks like. So let's rely on some auto complete response dot sure. Response. Great. Gonna continue returning that as an error. And over here, I'm going to quickly hook up an error state thing. So I'm not sure if I have any errors today. I do not. But somewhere here, maybe I have error message. Yeah. And I component for this. I have absolutely no idea what this takes, though. Let's go with some state. AI error set AI error equals used state. So on submission of this thing, I'm gonna say, okay. There is now no longer any AI error. No. But then when I get back a response, I'm just gonna chuck that into the response dot error. So maybe to make this conditional a little bit. So if response dot error, then do this. Otherwise, do that and update the filters to do update filters. Okay. It doesn't like this because it I haven't typed it. So I'm gonna say you're either no or a string. Mhmm. Initial null. Okay. And now I think I get to do like this, AI error, then render AI error. I've also clearly missed something around the start transition. I did. This is supposed to restart AI transition. Some of my text fix themselves. Okay. So a lot of UI plumbing, but let's see what we get now. If I say, hello. What happens? This thing spins. I expect it to spin for a minute because calling AI is not super quick. Hopefully, we get back something though. Woah. Okay. Bang. That's a react failure. Doesn't like me doing it inside. I don't know. Something about the order that these functions need to be called. I suspect this guy just needs to go somewhere. I'm going to cheat off myself and have a look at lesson three to see where I put this error message. I just put it in the wrong place. Switch filters. If I look for error message. Okay. It looks like it needs to go into a field is my guess. So just need to pull it up, up into this by this text error text area. And field set yeah. Okay. Let's do this. Something like that, maybe. Let's try that again. Hello. Get back some sort of response, and hopefully, it renders it rather than blows up. Yes. Okay. Fantastic. So this obviously still isn't our search filters, but it is now we're we've got some client form. We take the value of that. We pass it over to the server. The server is invoking an AI model, and this is still just using the default prompt that we had asking what is the origin of the phrase hello world. So I could type goodbye, it would still give me the exact same thing. But we're demonstrating that we have we're running AI in our local development server, and we're able to return response to our UI. So we are very almost there. All we need to do now is adjust the prompt on the server side saying like, okay. Here is the user's, like, raw input for searching properties, and here is the functions for searching properties and the parameters we expect you to call with. And then in the UI, map those parameters that the AI wants to call with to our search filters that exist today. We're running short on time, so I'm just gonna keep going so that we get to the other lessons. But the ballpark is you can say, you're our friendly assistant. You must call the search properties tool. You must not ask clarifying questions. Do not assume any information beyond what is provided. These are just like the instructions to the AI trying to get it to just immediately call our tool and not to, like I say, go back to the user with follow ups or anything like that. We just wanna go straight from single prompt to tool call, and we can extract that information that we need. Something along those lines. Again, lesson three has a sort of refined one that I'm I know works. The prompt, the user prompt in this case is just going to be the prompt that we get from the user up here. And, we're providing messages. Great. We also need to provide tools. And I have absolutely no idea again what the shape of these things are, but I think it's something like well, let's just go to the docs. So tools has a set of, it's an object with name, description, parameters. This is the interesting one. And, properties. Okay. So name, this is gonna be search properties. Description can be should be used for a given users For, to turn a set of properties given users search query, something like that. And then what's the other one? Parameters. And that is an object. Yep. So this is going to be type object and properties. Woah. Properties. This is where we get to define all of the filters that we have above. So let's take a look at that. So I think it's something like, bedrooms. And then I would say, like, type, yeah, is a number and description is blah blah blah required, false, that sort of thing. Let me again, we're just a bit short on time, so I'm gonna cheat and I'm just gonna pull this from lesson three, but all of this stuff is defined here in this property search file. And so we have exactly this, the description, the fact that it's an integer, minimum, maximum, fact it's optional, all that sort of stuff. So you could grab it straight from there, but I'm gonna cheat, and I'm going to copy it from lesson three. Here we go. So we again, like I say, we're defining bunch of messages upfront. We're passing in the search text directly. Here is the tool call. I'm just gonna grab this because there's some other stuff I wanna briefly talk about. So let me replace the object I was working on with this guy. Okay. So this is the full set of search filters that we had. And now I'm just gonna JSON stringify this response for a minute and see what we get back. Just the whole thing, because I expect it to be a tool call rather than a response response. Okay. So let me refresh things and say, I need a vacant two bedroom apartment with a garden. Okay. And now what I expect to get back, fingers crossed, is a tool call. Nice. Okay. A tool call with some arguments. The occupancy should be vacant. The bedroom should be two. There should be a garden, and it's calling the search for property tool. Amazing. That's exactly what I wanted. So now we just need to turn this thing into these search filters. And again, because I'm running a little bit low on time, I'm gonna cheat, a bit here. So let me just grab the rest of this. All this is doing is just saying, like, parsing this thing and saying if it didn't call a tool, call return the nice error. So I'm just gonna grab this and check it into AI search. And if we go back to the search filters, UI piece, This point to how this is working. So here, we have the AI search submit function. And again, we were, like, extracting out the the value, and we were calling AI search with it, extracting the error or filters that had been defined. And we're just setting the filters in the same sort of way that we were for the searching we're already doing. So there's nothing particularly special there. So again, let me just copy this, chuck this into lesson two, and move on. Hopefully, I haven't broken anything with these big changes. So mine looks much nicer. It has a nice sparkle thing. So let's go to bedrooms in Brooklyn. Click search. And what this should do is it should update our filters here on the left, which has two bedrooms in Brooklyn. Fantastic. Hasn't changed anything else. And now I can apply my filters and get my updated set of results. So this is I think, really important thing about AI right now is although we could have just given the entire set of properties to the LLM along with the user prompt, and it would have been able to just return text response saying like these ones or whatever. That's great in a chatbot. But the thing about chatbots is they're so inscrutable. You can't see how these things are working. And so for a really, like, mission critical service, obviously, this rental agency is not bad. But if you're building something really important, something like an observability dashboard where you need to be able to know for sure exactly what this thing is. It's not hallucinating, not imagining properties or logs that aren't there. You really need to use AI as an intermediary step between sort of user prompting or whatever that input is to completing with still that same structured data searching and all that sort of stuff that you're using today in applications. It's really important that it's just that intermediary so that you can see the input and the output, and know that it's not just making stuff up. So that's pretty much lesson two, I think. Let me just double check with the readme that I'm not missing anything, but I don't think so. So we made the UI. We made the server function. We called the AI with the synthetic tool calling because remember, the AI wasn't actually doing any of that search. We were still doing it. But we got the extracted structured data out of the the LLM. We ran it. I just got to deploy this thing, which is fine. I can do that. MPM run build and MPX Wrangler deploy. Okay. And twenty minutes, I think I'm going to have to just go straight into lesson three. Sorry. This was originally a bit of a longer workshop, so I've had to condense it down. But if you are wanting to, please, feel free to obviously work through this at your own pace. But I'm just gonna go straight into lesson three and start talking about it. As soon as I validate that this thing works in Proud, Two bedrooms in Brooklyn. And hopefully, we see our search filters update. Woah. Two bedrooms Brooklyn. Yep. Okay. Apply filters. Yeah. Perfect. So lesson two complete. Let me close all of my tabs. Come out of lesson two. Go into lesson three. Now this one is a lot shorter than lesson two. Lesson two was really the big beefy one. But lesson three, if I pull up its readme and make it pretty, is about MCP, the shirt that I'm wearing right now. The model context protocol. This is a standard which has been adopted by pretty much all of the big AI labs, and it is a way for your LLM to integrate with services and resources outside of just the chat conversation that has got. So in the early days, it's a bit old now, but in the early days, when LLMs first started, you had to copy and paste in any information you need you wanted this thing to know. And then it sort of had the ability to call the like, do a web search and stuff like this, and it started to get rudimentary access to the outside world. And then MCP came along, and this is the standard for which you expose applications to AIs. And there's two sides to it. There's an MCP server and an MCP client. The AI in this case is acting as the client, and so it gets to query a business or an application for information. And the business acts as a server and says, oh, I've got x and z, y property or whatever. And it's very much just like an API. It's an API for AIs. It's a fancy one, and we'll talk about why in just a minute. But it's basically just that communication protocol. So a couple of things to start with. We're going to install a couple of tools. We're in lesson three. Perfect. Clear and install them. Now these are published by the protocol standard peoples. And that doesn't look very good. Cool. So we got the model context protocol SDK. That's obviously just a low level SDK we're gonna be creating with. The inspector is a nice tool that we're gonna use to sort of see what's going on when we're using NCP. And agents, woah, a library that we, Cloudflare, publish. And it's just a nice interface sort of acting over the SDK for making it simple to implement MCP servers and other AI tooling on Cloudflare's developer platform. So it does lots, and we're barely scratching the surface with it today. But agents is a pretty cool library to check out if you're interested in AI beyond the stuff that we cover here today. In order to make agents work and create the MCP server that we need to, we're gonna need to use something called durable objects. Durable objects, again, do an awful lot. We're barely scratching the surface with them today. All they basically do in this universe is just provide a little bit of state for our server to run on. They can do, like I say, tons and tons more. They're very, very, very cool primitive, but we just need to copy and paste this piece of config, chuckle it in our Wrangler TOML, and that is enough to give the date for the agents to agents library to run on. So let's regen our types, and we're gonna create an MCP server. Now in this, all I've done is just give you a copy paste because it's basically straight out of the read me of agents library anyway. So if we're going to oh my goodness, worker lesson three, that's us. We're gonna replace our worker with this, copy paste snippet, and we can have a look at what's changed. So, right at the top, we are defining this class MCP, extending an MCP agent that's coming from the agents library. And we're giving it a name and a version, and we're saying, hey, it has this tool just adding two numbers together, a and b. Not very exciting, but this is just the boilerplate necessary to start up a server. And here, we're still returning the sort of React application, by default. But if you have a MCP or SSE path, then we're gonna do the AI stuff. So this way, we get to run both our React application as it stands today, but we're also exposing a MCP server, which will eventually hook into our business, on the same application. And that way we don't need to spin up something separate and manage it all independently, whatever. We can all combine them together in the same app, and use the same functions, all that same stuff. So I'll find it out. So let's run this thing. Cool. That's spun up on 8003. In a new tab, I'm going to also run up the woah. Inspector. And this, I think, opens a browser for me. Perfect. And so you need to set this up, to connect to your now running MCP server on 8003. So you select streamable HTTP. The others are old versions. Don't worry about them. So streamable HTTP is the newest thing. We're gonna say, hey. This thing is available on 8,003 slash MCP. That was the carve out that we made inside of our worker, if you remember here. I think that's fine. You don't need any authentication. If it's asking you for that, you hit connect. And hopefully, cool, it managed to connect and say we're we're live. It pulled that information about what the server is. Since we're at rental agency, we can also list the tools. And so here we found the one tool that we registered, this add tool. We get this lovely UI for interacting with this tool. We run it. We get back a result three. Okay. Not very exciting, but, the reason I'm showing you this is a, MCP does a whole bunch of stuff. Tools is just one of them, but all these things along the top are other things you can do inside MCP. So things like resources, provide prompts to the AI as your business. You can do things like sampling and elicitation, which is when you sort of push some of the work back over to the client over MCP. So it's a really very clever protocol, which is self describing. It says it gets to define what the tools are and what all these resources are that it has access to. And the AI can just sort of natively understand it because it's been trained on this stuff. So take a look at the MCP spec if you're interested in its capabilities, but there's tons and tons here. Now all we need to do is just rather than this boiling math tool, we need to have a search properties tool. And that way we can interact with our property listings both from our React application as we've just done. And but also from other MCP clients, things like ChatGPT. So, I'm gonna cheat again because I'm running low on time. But if we go into worker and lesson four, look how many lines this is. This is like 20 lines, and all I'm doing is replacing the add tool with a whatever this is called. My brain has just left me. Search property tool. Here we go. So I have no idea what I just copy pasted, but let's go with that. Yep. And I need to obviously copy these things. I'm just gonna copy the whole file. It's just the same. So we're calling the property search thing that we already had. We know that this is using the structured data. So this is still all the drizzle stuff. So it's not imagining things. Doesn't this invent, properties is querying our real data? And we define the right now, we're just returning kind of a a dump of it to the AI, but that's good enough for now. It can still unpick that and present it in a reasonable way. So sorry. That was a lot, but we are running very short on time, so I just need to keep pressing forward. Let's build this thing. I think that was everything in the readme, was it? Inspector, implement property search, test it locally, build it, and deploy it. Yep. And it's going to the right lesson. Lesson three. Run build and deploy it. Okay. Hopefully, some of you at least have been able to just about follow along either just in words or literally get some of this code working. But we deploy this thing, we go to the URL. Sure enough, we still have our React application. This all still works. Fantastix. But if we go to this same thing slash m c p, we get, like, a what what are you? You're not a robot error message. That's fine. This isn't built for humans to consume. This is built for m c p clients. So pick your favorite, AI thing, your AI chatbot. You can even use the playground here. You can go in here and you can chuck in the, URL that you've deployed this thing to, and sure enough, it detects the MCP, stuff. I'm gonna use ChatGPT because that's the most fun one, and I'm going to temporarily turn off my corporate VPN thing. If you go into your settings and you go into wherever this is, apps, you can go into the advanced settings, turn on developer mode. You get to connect this up to your own application. And so I just pasted in, again, my URL. We don't need authentication, all that sort of stuff. I've just set this up already, so I don't need to do this again. But you're able to hook up the application you deployed to something like ChatJPT or Cloud or whatever. And you can now ask this AI agent to interact with your business. And so we're saying, hey, use this MCP server. Usually, I can find it, but I'm just being very explicit here. So find me two bedroom properties, which have a garden. And now, all the fingers and toes crossed, it finds the tool. Okay. It's like thinking about it. It's calling a tool. It called search for property through my MCP server with the following properties. And then it got back a bunch of data, which again, we could return it in a better way, but the AI is good enough at parsing this out. And then, hey, presto. It's managed to grab those real structured property data things from our business and expose it to a user in a brand new way. And so this is, obviously very cool. A lot of people are using chatbots. For businesses, it's going to be, really hard to get, like SEO is kind of dying as a concept really now because everyone is interacting with chatbots rather than Google. And so if you want to expose your business and your application and your platform to users in this new way, like, you really need to start thinking about how you can expose your data and your functionality through things like MCP. And right now, this is really boring. It's just a read only search thing. But like I say, MCP handles tons. You can create UI components and return them directly inside chat. You can authenticate and you can be a real logged in user. You can take right actions. You can do something like creating property listings or whatever. So it's a really very powerful paradigm, and we've only just sort of scratched the surface with this read only implementation. Let's see if I can just very quickly make this work too. So let's see if it works in the playground. I'm going to this may this might not work. I haven't adjusted the system prompt or anything like that to really hone it in, but who knows? Maybe. Maybe not. Okay. Let's say not, but I will leave you with lesson four. Like I say, this workshop here has just been a introduction to some of these things, synthetic tool calling and MCP servers. But, there's tons of other very natural ways you could extend this. So, as a sort of easy option, we've looked at properties, but you get to do the same for tenants or whatever, other concepts within your business. Take a look at the MCP spec. Look at all of the functionality that that thing has. We've talked about tools, and we're just returning this data as a JSON dump. But alternatively, you could turn that as real resources or you could expose some UI controls, all that fun stuff. And this is unauthenticated, but MCP supports authentication and you can log in so that only your realtors have access to using this MCP server. Or, and this is the most fun one, you could take it a huge step forward and not only expose an MCP server, but you could start to become an MCP client yourself. You could interact with some other businesses' MCP servers. So the example I have here is that you have some legal agency who are responsible for drafting up your tenancy contracts or whatever. You could create an agent, which is able to, like, create new property listings and also interact with this other service, ask for a contract to be drafted up, return that, present it to the users and they can sign it or whatever it is. So you can really start to act in the middle of these things rather than just as something which gets exposed to AI. You can both consume AI as well. Cool. I think that's pretty much it. And I know I really pushed through with the last few stuff, but all this code is still gonna be up on my GitHub forever. The recording will go out so you can pick through that if there was any sort of use that you found to it. I did manage to get that Google format, which I'm going to post again, which I would very much appreciate if you have a moment to, share your thoughts on content of this workshop, any other feedback you have for me running it, that would be really very helpful for me. So thank you very much for attending. I hope you did learn something interesting. And if this is your first sort of foray into AI, fantastic. Please take it from here and, like, run with it because it's very exciting. Or if you've already been playing around, like I say, hopefully, was at least something new here for you that you can go and explore in a bit more detail. But, yeah, I think that's everything I had unless anybody had some last minute questions they wanted to throw my way. But thanks again for everyone attending. I had a lot of fun. Cool. I will leave you with that. Bye, folks.