Tool: publish
Publish a series of one or more messages to an existing topic.
Parameters
topic: The name of the topic to publish to in the formatprojects/{project_id}/topics/{topic_name}.messages: A list of one or more messages to publish. Each message must be of typebytes. If you are given a string, you must first convert it to bytes.
Returns
A PublishResponse object containing the message IDs of the published messages, if successful.
A
NOT_FOUNDerror if the topic does not exist.
Usage
Ensure that the topic exists by calling
get_topic.If the topic does not exist, return an error.
Create a new byte array to hold the message data.
Populate the byte array with the message data.
Call
publishpassing the topic name and the byte array.
Important Notes
- A project ID, topic name, and message data must be provided.
The following sample demonstrate how to use curl to invoke the publish MCP tool.
| Curl Request |
|---|
curl --location 'https://pubsub.googleapis.com/mcp' \ --header 'content-type: application/json' \ --header 'accept: application/json, text/event-stream' \ --data '{ "method": "tools/call", "params": { "name": "publish", "arguments": { // provide these details according to the tool's MCP specification } }, "jsonrpc": "2.0", "id": 1 }' |
Input Schema
Request for the Publish method.
PublishRequest
| JSON representation |
|---|
{
"topic": string,
"messages": [
{
object ( |
| Fields | |
|---|---|
topic |
Required. The messages in the request will be published on this topic. Format is |
messages[] |
Required. The messages to publish. |
PubsubMessage
| JSON representation |
|---|
{ "data": string, "attributes": { string: string, ... }, "messageId": string, "publishTime": string, "orderingKey": string } |
| Fields | |
|---|---|
data |
Optional. The message data field. If this field is empty, the message must contain at least one attribute. A base64-encoded string. |
attributes |
Optional. Attributes for this message. If this field is empty, the message must contain non-empty data. This can be used to filter messages on the subscription. An object containing a list of |
messageId |
ID of this message, assigned by the server when the message is published. Guaranteed to be unique within the topic. This value may be read by a subscriber that receives a |
publishTime |
The time at which the message was published, populated by the server when it receives the Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: |
orderingKey |
Optional. If non-empty, identifies related messages for which publish order should be respected. If a |
AttributesEntry
| JSON representation |
|---|
{ "key": string, "value": string } |
| Fields | |
|---|---|
key |
|
value |
|
Timestamp
| JSON representation |
|---|
{ "seconds": string, "nanos": integer } |
| Fields | |
|---|---|
seconds |
Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be between -62135596800 and 253402300799 inclusive (which corresponds to 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z). |
nanos |
Non-negative fractions of a second at nanosecond resolution. This field is the nanosecond portion of the duration, not an alternative to seconds. Negative second values with fractions must still have non-negative nanos values that count forward in time. Must be between 0 and 999,999,999 inclusive. |
Output Schema
Response for the Publish method.
PublishResponse
| JSON representation |
|---|
{ "messageIds": [ string ] } |
| Fields | |
|---|---|
messageIds[] |
Optional. The server-assigned ID of each published message, in the same order as the messages in the request. IDs are guaranteed to be unique within the topic. |
Tool Annotations
Destructive Hint: ❌ | Idempotent Hint: ❌ | Read Only Hint: ❌ | Open World Hint: ✅