1. Introduction
Overview
In this lab, you will build and deploy a Model Context Protocol (MCP) server using mcp-use TypeScript. MCP servers are useful for providing LLMs with access to external tools and services. You will configure it as a secure, production-ready service on Cloud Run that can be accessed from multiple clients. Then you will connect to the remote MCP server from Gemini CLI.What you’ll do
We will use mcp-use TypeScript to create a zoo MCP server that has two tools:get_animals_by_species and get_animal_details. mcp-use provides a quick, type-safe way to build MCP servers with full TypeScript support.
What you’ll learn
- Deploy the MCP server to Cloud Run
- Secure your server’s endpoint by requiring authentication for all requests, ensuring only authorized clients and agents can communicate with it
- Connect to your secure MCP server endpoint from Gemini CLI
- Build ChatGPT Apps with interactive widgets
2. Project Setup
- If you don’t already have a Google Account, you must create a Google Account.
- Use a personal account instead of a work or school account. Work and school accounts may have restrictions that prevent you from enabling the APIs needed for this lab.
- Sign-in to the Google Cloud Console.
- Enable billing in the Cloud Console.
- Completing this lab should cost less than $1 USD in Cloud resources.
- You can follow the steps at the end of this lab to delete resources to avoid further charges.
- New users are eligible for the $300 USD Free Trial.
- Create a new project or choose to reuse an existing project.
3. Open Cloud Shell Editor
- Click this link to navigate directly to Cloud Shell Editor
- If prompted to authorize at any point today, click Authorize to continue.
- If the terminal doesn’t appear at the bottom of the screen, open it:
- Click View
- Click Terminal
- In the terminal, set your project with this command:
- Format:
- Example:
- If you can’t remember your project id:
- You can list all your project ids with:
- You can list all your project ids with:
- Format:
- You should see this message:
If you see a
WARNINGand are askedDo you want to continue (Y/n)?, then you have likely entered the project ID incorrectly. Pressn, pressEnter, and try to run thegcloud config set projectcommand again.
4. Enable APIs
In the terminal, enable the APIs:5. Create the zoo MCP server
To provide valuable context for improving the use of LLMs with MCP, set up a zoo MCP server with mcp-use TypeScript — a standard framework for working with the Model Context Protocol. mcp-use provides a quick, type-safe way to build MCP servers with full TypeScript support. This MCP server provides data about animals at a fictional zoo. For simplicity, we store the data in memory. For a production MCP server, you probably want to provide data from sources like databases or APIs.-
Create a new MCP server project using
create-mcp-use-app:This command will:- Create a complete TypeScript MCP server project structure
- Install all necessary dependencies (mcp-use, zod, TypeScript, React, etc.)
- Set up pre-configured build tools and dev server
- Create example files to get you started
-
Open the MCP server entry point file:
- Replace the contents with the following zoo MCP server source code:
create-mcp-use-app template already includes the necessary imports and server setup. We’ve replaced the example code with our zoo server implementation.
6. Building ChatGPT Apps with Your MCP Server
One of the powerful features of mcp-use is its built-in support for building ChatGPT Apps with interactive widgets. This allows you to create rich, interactive user interfaces that go beyond simple text responses.What are ChatGPT Apps?
ChatGPT Apps enable developers to build rich, interactive user interfaces using the OpenAI Apps SDK. Widgets can:- Display data in tables, carousels, or custom layouts
- Allow users to filter, sort, or manipulate results
- Persist state across conversations
- Call MCP tools directly from the UI
Adding a Widget to Your Zoo Server
Let’s add an interactive widget to display animal information in a beautiful card format.-
The
resourcesdirectory should already exist fromcreate-mcp-use-app. Create a widget componentresources/animal-card.tsx: - Add the following widget code:
- Update your
src/index.tsto return widgets from tools. Add this new tool that returns a widget:
Widget Features
With mcp-use widgets, you get:- Automatic Registration: Widgets in the
resources/folder are automatically discovered - Type Safety: Full TypeScript support with Zod schema validation
- State Management: Built-in state persistence with
useWidget()hook - Tool Calls: Widgets can call other MCP tools directly
- Hot Reload: Changes reflect immediately during development
7. Deploying to Cloud Run
Now deploy an MCP server to Cloud Run directly from the source code.-
Create and open a new
Dockerfilefor deploying to Cloud Run: - Include the following code in the Dockerfile:
-
Create a
.dockerignorefile to exclude unnecessary files: -
Create a service account named
mcp-server-sa: - Add storage.objectViewer role to the service account:
-
Run the
gcloudcommand to deploy the application to Cloud Run:Use the--no-allow-unauthenticatedflag to require authentication. This is important for security reasons. If you don’t require authentication, anyone can call your MCP server and potentially cause damage to your system. -
Confirm the creation of a new Artifact Registry repository. Since it is your first time deploying to Cloud Run from source code, you will see:
Type
Yand pressEnter, this will create an Artifact Registry repository for your deployment. This is required for storing the MCP server Docker container for the Cloud Run service. -
After a few minutes, you will see a message like:
8. Add the Remote MCP Server to Gemini CLI
Now that you’ve successfully deployed a remote MCP server, you can connect to it using various applications like Google Code Assist or Gemini CLI. In this section, we will establish a connection to your new remote MCP server using Gemini CLI.-
Give your user account permission to call the remote MCP server:
-
Save your Google Cloud credentials and project number in environment variables for use in the Gemini Settings file:
-
Make a
.geminifolder if it has not already been created: -
Open your Gemini CLI Settings file:
-
Replace your Gemini CLI settings file to add the Cloud Run MCP server:
-
Start the Gemini CLI in Cloud Shell:
You may need to press
Enterto accept some default settings. -
Have gemini list the MCP tools available to it within its context:
-
Ask gemini to find something in the zoo:
The Gemini CLI should know to use the
zoo-remoteMCP Server and will ask if you would like to allow execution of MCP. -
Use the down arrow, then press
Enterto select:
/quit and then press Enter to exit Gemini CLI.
Debugging
If you see an error like this:
ID_TOKEN again.
- Type
/quitand then pressEnterto exit Gemini CLI. - Set your project in your terminal:
- Restart on step 2 above
9. (Optional) Verify Tool Calls in Server Logs
To verify that your Cloud Run MCP server was called, check the service logs.10. (Optional) Add MCP prompt to Server
An MCP prompt can speed up your workflow for prompts you run often by creating a shorthand for a longer prompt. Gemini CLI automatically converts MCP prompts into custom slash commands so that you can invoke an MCP prompt by typing/prompt_name where prompt_name is the name of your MCP prompt.
Create an MCP prompt so you can quickly find an animal in the zoo by typing /find animal into Gemini CLI.
- Add this code to your
src/index.tsfile above the server.listen() call:
-
Re-deploy your application to Cloud Run:
-
Refresh your ID_TOKEN for your remote MCP server:
-
After the new version of your application is deployed, start Gemini CLI:
-
In the prompt use the new custom command that you created:
OR
get_animals_by_species tool and formats the response as instructed by the MCP prompt!
11. Use Gemini Flash Lite for faster responses (Optional)
Gemini CLI lets you choose the model you are using.- Gemini 2.5 Pro is Google’s state-of-the-art thinking model, capable of reasoning over complex problems in code, math, and STEM, as well as analyzing large datasets, codebases, and documents using long context.
- Gemini 2.5 Flash is Google’s best model in terms of price-performance, offering well-rounded capabilities. 2.5 Flash is best for large scale processing, low-latency, high volume tasks that require thinking, and agentic use cases.
- Gemini 2.5 Flash Lite is Google’s fastest flash model optimized for cost-efficiency and high throughput.
-
After the new version of your application is deployed, start Gemini CLI:
-
In the prompt use the new custom command that you created:
get_animals_by_species tool and formats the response as instructed by the MCP prompt, but the answer should appear much faster!
Debugging
If you see an error like this:/mcp and if it outputs zoo-remote - Disconnected, you might have to re-deploy, or run the following commands again:
Conclusion
Congratulations! You have successfully deployed and connected to a secure remote MCP server built with mcp-use TypeScript.What You’ve Accomplished
- ✅ Created a production-ready MCP server with TypeScript
- ✅ Deployed it securely to Google Cloud Run with authentication
- ✅ Connected to it from Gemini CLI
- ✅ Added interactive ChatGPT Apps widgets
- ✅ Learned how to use MCP prompts for faster workflows
Continue Learning
This lab demonstrates the fundamentals of deploying MCP servers. You can extend this by:- Adding more tools and resources
- Integrating with databases or external APIs
- Building more complex ChatGPT Apps widgets
- Adding authentication and authorization
- Scaling your server for high traffic
(Optional) Clean up
If you are not continuing on to the next lab and you would like to clean up what you have created, you can delete your Cloud project to avoid incurring additional charges. While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Artifact Registry. Deleting your Cloud project stops billing for all the resources used within that project. If you would like, delete the project:-
Delete the codelab project directory:
-
Warning! This next action can’t be undone! If you would like to delete everything on your Cloud Shell to free up space, you can delete your whole home directory. Be careful that everything you want to keep is saved somewhere else.