This sample demonstrates how to create a proxy between a client application and Google Cloud's Agent Engine using Firebase Cloud Functions. It allows secure access to agents built with the Agent Development Kit (ADK) from customer-facing applications.
Accessing Agent Engine directly from a client application requires service account credentials or user credentials with broad access, which is not secure for public-facing apps. This wrapper provides:
- Authentication: Automatically integrates with Firebase Authentication.
- Security: Enforces App Check to prevent abuse.
- Encapsulation: Hides specific Agent Engine IDs and project details from the client.
See the functions/src/adk-endpoints directory for the implementation of each endpoint.
All functions are implemented as Callable Functions (onCall). They automatically decode client data and verify user authentication tokens.
To call these functions from a client app (e.g., Web, iOS, Android), use the Firebase Functions SDK. Here is a generic example using the JS SDK:
import { getFunctions, httpsCallable } from 'firebase/functions';
const functions = getFunctions();
// Example: Calling async_create_session
const asyncCreateSession = httpsCallable(functions, 'async_create_session');
try {
const result = await asyncCreateSession();
console.log('Session created:', result.data);
} catch (error) {
console.error('Error creating session:', error);
}Here are the available callable functions and how to call them with data.
Creates a new session for the authenticated user.
- Requires Auth: Yes
- Input: None (Uses the authenticated user's UID as
user_id). - Returns: The created session object.
Deletes a session for the authenticated user.
- Requires Auth: Yes
- Input:
{ session_id: string } - Returns: The result from Agent Engine.
Retrieves details for a specific session.
- Requires Auth: Yes
- Input:
{ session_id: string } - Returns: The session details.
Lists all sessions for the authenticated user.
- Requires Auth: Yes
- Input: None (Uses the authenticated user's UID to filter sessions).
- Returns: An array of sessions.
Adds a session to memory (generates memories).
- Requires Auth: Yes
- Input:
{ session: any } - Returns: The result from Agent Engine.
Searches memories for the given user.
- Requires Auth: Yes
- Input:
{ query: string } - Returns: The search results.
Streams responses asynchronously from the ADK application.
- Requires Auth: Yes
- Input:
{ message: string, session_id?: string, run_config?: any } - Returns: An object containing the full response and chunks.
Streams responses asynchronously from the ADK application, typically used by tools like AgentSpace.
- Requires Auth: Yes
- Input:
{ request_json: any } - Returns: An object containing the full response and chunks.
The functions/src/common folder contains shared logic and configuration for all endpoints.
adk.ts: Contains helper functionscallReasoningEngineandcallReasoningEngineStreamthat use the@google-cloud/aiplatformSDK to communicate with Agent Engine.config.ts: Defines the configuration options for the project.
To use this wrapper, you need to configure it with your Google Cloud and Agent Engine details. You can do this by setting environment variables or editing the values directly in config.ts:
PROJECT_ID: The Google Cloud project ID containing your agent. Defaults toprocess.env.GCLOUD_PROJECT.LOCATION: The region where your Agent Engine agent is deployed (e.g.,us-central1). Defaults toprocess.env.LOCATION.REASONING_ENGINE_ID: The unique ID of your reasoning engine instance. Defaults toprocess.env.REASONING_ENGINE_ID.ENFORCE_APP_CHECK: Set totrueto require Firebase App Check tokens for all requests. Hardcoded totruein this sample.REPLAY_PROTECTED: Set totrueto consume App Check tokens for replay protection. Hardcoded totruein this sample.
To set up the sample:
- Create a Firebase Project using the Firebase Console.
- Enable Cloud Functions and Firebase Authentication.
- Deploy your ADK agent to Agent Engine and obtain the
REASONING_ENGINE_ID. - Clone this repository.
- Navigate to this sample directory:
cd Node/adk-wrapper. - Set up your project:
firebase use --addand follow the instructions. - Install dependencies:
cd functions; npm install; cd -. - Set environment variables or edit
functions/src/common/config.tswith your values. - Deploy the functions:
firebase deploy.