@azure/functions package

類別

AppStartContext

應用程式啟動時函式應用程式的上下文。

AppTerminateContext

應用程式終止時函式應用程式的上下文。

Disposable

代表一種可釋放資源的類型,例如事件監聽或計時器。

HookContext

所有掛鉤上下文物件的基底類別

HttpRequest

HTTP 要求物件。 使用 HTTP 系結時提供給您的函式。

HttpResponse

HTTP 回應類別

InvocationContext

包含此調用專屬的元數據和協助程式方法

InvocationHookContext

所有呼叫鉤子上下文物件的基底類別

LogHookContext

記錄中的上下文

McpAudioContent

MCP 工具回應中的音訊內容區塊。

McpContentBlock

MCP 工具回應內容區塊的抽象基底類別。

Azure Functions 函式庫會使用 instanceof McpContentBlock,將內容區塊與純使用者值區分開來,因此只有內建子類別(McpTextContent、McpResourceLinkContentMcpImageContentMcpAudioContentMcpResourceContent)的實例——或是延伸此類別的自訂子類別——才會被視為內容區塊。

像 { type: 'text', text: '...' } 是純物件的字面值 不會 被視為內容區塊,而是會被序列化成 JSON 文字。

以自訂內容區塊類型進行擴充

如果 MCP 規範新增了新的區塊類型——或你的情境需要自訂的——你可以在不更改任何函式庫的情況下推出自己的子類別。 轉換器只會檢查 instanceof McpContentBlock 並呼叫 JSON.stringify(block),這會促使你 toJSON() 產生線材有效載荷。

範例:新增一個假設性情形 VideoContent

import { McpContentBlock } from '@azure/functions';

export interface VideoContentInit {
    data: string | Buffer | ArrayBuffer;
    mimeType: string;          // e.g. 'video/mp4'
    durationMs?: number;       // optional field from a hypothetical spec
}

export class VideoContent extends McpContentBlock {
    readonly type = 'video' as const;
    readonly data: string | Buffer | ArrayBuffer;
    readonly mimeType: string;
    readonly durationMs?: number;

    constructor(init: VideoContentInit) {
        super();
        this.data = init.data;
        this.mimeType = init.mimeType;
        this.durationMs = init.durationMs;
    }

    toJSON(): Record<string, unknown> {
        const out: Record<string, unknown> = {
            type: this.type,
            data: toBase64(this.data),
            mimeType: this.mimeType,
        };
        if (this.durationMs !== undefined) out.durationMs = this.durationMs;
        return out;
    }
}

function toBase64(data: string | Buffer | ArrayBuffer): string {
    if (typeof data === 'string') return data;
    if (Buffer.isBuffer(data)) return data.toString('base64');
    return Buffer.from(new Uint8Array(data)).toString('base64');
}

使用工具處理程式中的自訂區塊

// Single block
handler: async () => new VideoContent({ data: buf, mimeType: 'video/mp4' })

// Mixed with built-ins + structured content
handler: async () => new McpToolResponse({
    content: [
        new McpTextContent('Detected 3 scenes'),
        new VideoContent({ data: buf, mimeType: 'video/mp4' }),
    ],
    structuredContent: { scenes: 3, confidence: 0.92 },
})

圖書館為你做的事

  • instanceof McpContentBlock 把你的子職業當成內建方塊一樣對待。
  • 單區塊回傳會將您的字串傳播 type 到外部結果;陣列則以 包裹為 multi_content_result。
  • structuredContent 處理、備援文字合成及 nullish 直通皆未變更。

你在子職業中必須掌握的重點

  • toJSON() 必須回傳規格中對您的 type.
  • 二進位有效載荷應該以 base64 編碼(toJSON()見上方輔助工具)。toBase64
  • 純物件字面值 無法 被識別——您必須建構一個實例。
McpImageContent

MCP 工具回應中的圖片內容區塊。

McpPromptArgumentBuilder

流暢建構器,可在記錄形狀 promptArguments 的地圖中宣告 MCP 提示參數。 映射的鍵提供參數名稱。

預設情況下,參數為 可選;呼叫 .isRequired() 以標記為必填。

範例

promptArguments: {
    function_name: promptArg.describe('The function to document.').isRequired(),
    style: promptArg.describe("Documentation style (e.g., 'concise', 'verbose')."),
}
McpResourceContent

MCP 工具回應中的嵌入資源內容區塊。

McpResourceLinkContent

MCP 工具回應中的資源連結內容區塊。

McpTextContent

MCP 工具回應中的文字內容區塊。

McpToolResponse

完整的 MCP 工具回應,包含明確的內容區塊與可選的結構化內容。 當你需要同時控制內容陣列與 structuredContent時,從工具處理器傳回此類別的實例。

return new McpToolResponse({
  content: [
    new McpTextContent('Here is the image'),
    new McpImageContent({ data: base64Data, mimeType: 'image/png' })
  ],
  structuredContent: { imageId: 'logo', format: 'png' }
});
PostInvocationContext

函式執行後的上下文。

PreInvocationContext

函式執行前的上下文。

PromptInvocationContext

傳遞給 MCP 提示程式處理器的結構化上下文物件。

提供對被喚出提示字元名稱、參數(所有值皆為字串)、可選的工作階段 ID 及可選傳輸元資料的類型存取。

範例

app.mcpPrompt('codeReview', {
    promptName: 'code_review',
    promptArguments: [
        { name: 'code', description: 'The code to review', required: true },
        { name: 'language', description: 'Programming language' }
    ],
    handler: async (ctx: PromptInvocationContext) => {
        const code = ctx.arguments.code ?? '';
        const language = ctx.arguments.language ?? 'typescript';
        return `Review this ${language} code:\n${code}`;
    }
});

介面

AppStartContextInit

傳遞給 AppStartContext 建構子的物件。 僅供測試之用

AppTerminateContextInit

傳遞給 AppTerminateContext 建構子的物件。 僅供測試之用

ConnectorTriggerFunctionOptions
ConnectorTriggerOptions
Cookie

Http 回應 Cookie 物件為 “Set-Cookie”

CosmosDBv3FunctionOptions
CosmosDBv3InputOptions
CosmosDBv3OutputOptions
CosmosDBv3TriggerOptions
CosmosDBv4AllVersionsAndDeletesFunctionOptions
CosmosDBv4ChangeFeedItem

當 為 AllVersionsAndDeletes時,所發出changeFeedMode的全保真變更饋送項目有效載荷。 屬性名稱遵循 Cosmos DB 所發出的線路格式。

CosmosDBv4ChangeFeedMetadata

使用 AllVersionsAndDeletes時,每個變更資料饋送項目均包含元資料。 屬性名稱遵循 Cosmos DB 所發出的線路格式。

CosmosDBv4InputOptions
CosmosDBv4LatestVersionFunctionOptions
CosmosDBv4OutputOptions
CosmosDBv4TriggerOptions
Duration
EffectiveFunctionOptions

註冊函式時所使用的選項,如傳遞至特定調用附注:這個值可能會與原始值稍有不同,因為它已經過驗證,而且預設值可能已明確新增

EventGridEvent

文件與範例連結

EventGridFunctionOptions

目前沒有任何事件方格觸發程式特定選項

EventGridOutputConnectionOptions
EventGridOutputKeyOptions
EventGridPartialEvent

文件與範例連結 這個「部分」介面是設計給自己建立事件時使用的,並且允許省略部分屬性

EventGridTriggerOptions

目前沒有任何事件方格觸發程式特定選項

EventHubFunctionOptions
EventHubOutputOptions
EventHubTriggerOptions
Exception
ExponentialBackoffRetryOptions
FixedDelayRetryOptions
FunctionInput

函式次要輸入的完整組態 (“trigger” 是主要輸入) 注意:並非所有觸發程式都可以作為次要輸入使用

FunctionOptions

設定 Azure 函式的輸入、輸出和處理程式

FunctionOutput

函式輸出的完整組態

FunctionTrigger

函式主要輸入的完整組態

GenericFunctionOptions

設定 Azure 函式的輸入、輸出和處理程式

GenericInputOptions
GenericOutputOptions
GenericTriggerOptions
HookContextInit

傳遞給 HookContext 建構子的物件基礎介面。 僅供測試之用。

HttpFunctionOptions
HttpOutputOptions

此時沒有 HTTP 輸出特定選項

HttpRequestBodyInit

僅供測試之用。 在 Azure Functions 運行時間的內容中執行時,一律會為您建構此專案

HttpRequestInit

僅供測試之用。 在 Azure Functions 運行時間的內容中執行時,一律會為您建構此專案

HttpRequestUser

物件,表示透過AppService/Functions驗證或SWA驗證登入的使用者

HttpResponseInit
HttpTrigger

函式主要輸入的完整組態

HttpTriggerOptions
IToolPropertyBuilder

用於建立 MCP 工具屬性的 Fluent API 建構器介面,此介面作為實作與型別宣告之間的橋樑

InvocationContextExtraInputs

用來取得次要輸入的物件

InvocationContextExtraOutputs

用來設定次要輸出的物件

InvocationContextInit

僅供測試之用。 在 Azure Functions 運行時間的內容中執行時,一律會為您建構此專案

InvocationHookContextInit

傳入 InvocationHookContext 建構子的物件。 僅供測試之用

LogHookContextInit

傳入 LogHookContext 建構子的物件。 僅供測試之用

McpAudioContentInit

初始化器。McpAudioContent data 會自動以base64編碼來處理Buffer/ArrayBuffer的值。

McpImageContentInit

初始化器。McpImageContent data 會自動以base64編碼來處理Buffer/ArrayBuffer的值。

McpPromptArgument

描述由 MCP 提示字句接受的單一參數。

McpPromptFunctionOptions

MCP 提示函數的設定選項。 這包括觸發器專屬選項以及一般功能選項。

McpPromptTriggerOptions

MCP 提示字元觸發器的設定選項。 這些選項定義觸發器的行為與元資料。

McpPromptTriggerOptionsToRpc

傳送給 Functions 主機的 MCP 提示字元觸發器的線格式選項。 promptArguments 依照主機綁定合約的要求,序列化為 JSON 字串。

McpResourceContentInit

初始化器。McpResourceContent blob 會自動以base64編碼來處理Buffer/ArrayBuffer的值。

McpResourceFunctionOptions

MCP 資源函數的設定選項。 這包括觸發器專屬選項以及一般功能選項。

McpResourceLinkContentInit

初始化器。McpResourceLinkContent

McpResourceTriggerOptions

MCP 資源觸發器的設定選項。 這些選項定義觸發器的行為與元資料。

McpToolFunctionOptions

MCP 工具函數的設定選項。 這包括觸發器專屬選項以及一般功能選項。

McpToolProperty
McpToolResponseInit

初始化器。McpToolResponse

McpToolTriggerOptions

MCP 工具觸發器的設定選項。 這些選項定義觸發器的行為與元資料。

McpToolTriggerOptionsToRpc

MCP 工具觸發器的設定選項。 這些選項定義觸發器的行為與元資料。

MySqlChange
MySqlFunctionOptions
MySqlInputOptions
MySqlOutputOptions
MySqlTriggerOptions
PostInvocationContextInit

傳入 PostInvocationContext 建構子的物件。 僅供測試之用

PreInvocationContextInit

傳入 PreInvocationContext 建構子的物件。 僅供測試之用

RetryContext
ServiceBusQueueFunctionOptions
ServiceBusQueueOutputOptions
ServiceBusQueueTriggerOptions
ServiceBusTopicFunctionOptions
ServiceBusTopicOutputOptions
ServiceBusTopicTriggerOptions
SetupOptions
SqlChange
SqlFunctionOptions
SqlInputOptions
SqlOutputOptions
SqlTriggerOptions
StorageBlobFunctionOptions
StorageBlobOptions
StorageBlobTriggerOptions
StorageQueueFunctionOptions
StorageQueueOptions
TableInputOptions
TableOutputOptions
Timer

定時器排程資訊。 使用定時器系結時提供給函式。

TimerFunctionOptions
TimerTriggerOptions
TraceContext

啟用分散式追蹤案例的 TraceContext 資訊

WarmupContext
WarmupFunctionOptions
WarmupTriggerOptions
WebPubSubConnectionInputOptions
WebPubSubContextInputOptions
WebPubSubFunctionOptions
WebPubSubOutputOptions
WebPubSubTriggerOptions

類型別名

AppStartHandler

應用程式啟動鉤子的處理程式

AppTerminateHandler

應用程式終止掛鉤的處理程式

Arg

代表工具屬性定義(與 McpToolProperty 相同,但不含 propertyName)

Args

工具屬性格式——將物件屬性名稱映射到其定義

ConnectorTrigger
ConnectorTriggerHandler
CosmosDBChangeFeedItem
CosmosDBChangeFeedMetadata
CosmosDBFunctionOptions
CosmosDBHandler
CosmosDBInput
CosmosDBInputOptions
CosmosDBOutput
CosmosDBOutputOptions
CosmosDBTrigger
CosmosDBTriggerOptions
CosmosDBv3Handler
CosmosDBv3Input
CosmosDBv3Output
CosmosDBv3Trigger
CosmosDBv4FullFidelityFunctionOptions
CosmosDBv4FunctionOptions
CosmosDBv4Handler
CosmosDBv4Input
CosmosDBv4Output
CosmosDBv4Trigger
EventGridHandler
EventGridOutput
EventGridOutputOptions
EventGridTrigger
EventHubHandler
EventHubOutput
EventHubTrigger
FunctionHandler
FunctionResult

如果未註冊任何 return 輸出,則為 Void;否則,已註冊的 return 輸出

HttpHandler
HttpHeadersInit

代表可用於初始化 HTTP 標頭的標頭類型。 這是一個在地定義,以避免對 lib.dom 的依賴。 相容於 Node.js 原生的 Fetch API 標頭類型。

HttpMethod

HTTP 要求方法的可能值。

HttpMethodFunctionOptions
HttpOutput
HttpRequestParams

路由參數索引鍵和值。

HttpRequestUserType

HTTP 要求使用者類型的可能值

HttpResponseBodyInit

代表可用於 HTTP 回應的文字類型。 這是一個在地定義,以避免對 lib.dom 的依賴。 相容於 Node.js 原生 Fetch API 主體類型。 包含 Node.js 可讀串流與 HTTP 串流場景下的非同步可循序。

LogCategory
LogHandler
LogHookHandler

原木鉤的處理程式。

LogLevel
McpPromptArguments

宣告提示參數的接受形狀。

  • 陣列形式(明確):包含 、 可選description且 required的 McpPromptArgument 物件name陣列。
  • Record/Fluent 表單:一個物件,其鍵為參數名稱與值,皆為由 promptArg.describe(...)建置的 McpPromptArgumentBuilder 實例。
McpPromptTrigger

代表一個 MCP 提示詞觸發器,結合基本函數觸發選項與 MCP 提示字元專屬觸發器選項。

McpPromptTriggerHandler

用於 MCP 提示字元觸發器的處理函數。

McpResourceTrigger

代表一個 MCP 資源觸發器,結合基底函數觸發選項與 MCP 資源專屬觸發選項。

McpResourceTriggerHandler

用於 MCP 資源觸發器的處理函數。

McpToolTrigger

代表一個 MCP 工具觸發器,結合基本功能觸發選項與 MCP 工具專屬的觸發選項。

McpToolTriggerHandler

MCP 工具觸發器的處理函數。

MySqlHandler
MySqlInput
MySqlOutput
MySqlTrigger
PostInvocationHandler

用於呼叫後鉤子的處理程序

PreInvocationHandler

用於召喚前掛鉤的處理程式。

RetryOptions
ServiceBusQueueHandler
ServiceBusQueueOutput
ServiceBusQueueTrigger
ServiceBusTopicHandler
ServiceBusTopicOutput
ServiceBusTopicTrigger
SqlHandler
SqlInput
SqlOutput
SqlTrigger
StorageBlobHandler
StorageBlobInput
StorageBlobInputOptions
StorageBlobOutput
StorageBlobOutputOptions
StorageBlobTrigger
StorageQueueHandler
StorageQueueOutput
StorageQueueOutputOptions
StorageQueueTrigger
StorageQueueTriggerOptions
TableInput
TableOutput
TimerHandler
TimerTrigger
TriggerMetadata

與觸發函式之輸入相關的元數據

WarmupHandler
WarmupTrigger
WebPubSubConnectionInput
WebPubSubContextInput
WebPubSubHandler
WebPubSubOutput
WebPubSubTrigger

列舉

CosmosDBv4ChangeFeedMode

變更 Cosmos DB 觸發鍵綁定的饋送模式。

MySqlChangeOperation
SqlChangeOperation

函式

McpContent<T>(T)

將類別或建構函式標記為 MCP 結果型態,該類型應序列化為結構化內容。

當函式回傳以此標記裝飾的型別實例時,結果將同時序列化為文字內容(以符合向下相容性)及結構化內容(支援此格式的用戶端)。

若不使用標記,純物件會序列化為純文字(JSON 字串化)。 如果您需要在沒有類別的情況下明確控制 structuredContent,請使用此 McpToolResponse 方法。

程式化使用 (無需 experimentalDecorators使用):

class ImageMetadata {
  constructor(public imageId: string, public format: string) {}
}
McpContent(ImageMetadata);  // marks the class

app.mcpTool('GetImage', {
  toolName: 'GetImage',
  description: 'Returns image info',
  handler: async () => new ImageMetadata('logo', 'png')
});

裝飾器使用 (需 experimentalDecorators: true 在 tsconfig 中說明):

@McpContent
class ImageMetadata { ... }

支援的表單:

  • @McpContent
  • @McpContent()
  • McpContent(MyClass)

請參閱 McpToolResponse相關內容區塊 + 結構化內容 請參閱 McpTextContent關於回傳單一內容區塊

McpContent()

變數

arg

工廠函數用於建立新的工具屬性建構器

範例

const toolProperties = {
  snippetName: arg
    .string()
    .describe("Some Description"),

  optionalField: arg
    .number()
    .describe("Optional number field")
    .optional(),
};
promptArg

用於建立 MCP 提示 參數建構器的工廠。

由於提示參數未設定型別(僅name有 、 可選 description及一個required旗標),因此與工具屬性arg助手分開使用。

函式詳細資料

McpContent<T>(T)

將類別或建構函式標記為 MCP 結果型態,該類型應序列化為結構化內容。

當函式回傳以此標記裝飾的型別實例時,結果將同時序列化為文字內容(以符合向下相容性)及結構化內容(支援此格式的用戶端)。

若不使用標記,純物件會序列化為純文字(JSON 字串化)。 如果您需要在沒有類別的情況下明確控制 structuredContent,請使用此 McpToolResponse 方法。

程式化使用 (無需 experimentalDecorators使用):

class ImageMetadata {
  constructor(public imageId: string, public format: string) {}
}
McpContent(ImageMetadata);  // marks the class

app.mcpTool('GetImage', {
  toolName: 'GetImage',
  description: 'Returns image info',
  handler: async () => new ImageMetadata('logo', 'png')
});

裝飾器使用 (需 experimentalDecorators: true 在 tsconfig 中說明):

@McpContent
class ImageMetadata { ... }

支援的表單:

  • @McpContent
  • @McpContent()
  • McpContent(MyClass)

請參閱 McpToolResponse相關內容區塊 + 結構化內容 請參閱 McpTextContent關於回傳單一內容區塊

function McpContent<T>(target: T): T

參數

target

T

可選的類別或建構函數,用於標記結構化內容。

傳回

T

目標類別/函式不變,或是類別裝飾器。

McpContent()

function McpContent(): (target: T) => T

傳回

(target: T) => T

變數詳細資料

arg

工廠函數用於建立新的工具屬性建構器

範例

const toolProperties = {
  snippetName: arg
    .string()
    .describe("Some Description"),

  optionalField: arg
    .number()
    .describe("Optional number field")
    .optional(),
};
arg: { boolean() => IToolPropertyBuilder, datetime() => IToolPropertyBuilder, double() => IToolPropertyBuilder, integer() => IToolPropertyBuilder, long() => IToolPropertyBuilder, number() => IToolPropertyBuilder, object() => IToolPropertyBuilder, string() => IToolPropertyBuilder }

類型

{ boolean() => IToolPropertyBuilder, datetime() => IToolPropertyBuilder, double() => IToolPropertyBuilder, integer() => IToolPropertyBuilder, long() => IToolPropertyBuilder, number() => IToolPropertyBuilder, object() => IToolPropertyBuilder, string() => IToolPropertyBuilder }

promptArg

用於建立 MCP 提示 參數建構器的工廠。

由於提示參數未設定型別(僅name有 、 可選 description及一個required旗標),因此與工具屬性arg助手分開使用。

promptArg: { describe(description: string) => McpPromptArgumentBuilder }

類型

{ describe(description: string) => McpPromptArgumentBuilder }