Skip to content

Add MCP tool for retrieving instant view from Redis messages#705

Merged
hugefiver merged 5 commits into
devfrom
copilot/add-mcp-tool-for-instant-view
Jan 30, 2026
Merged

Add MCP tool for retrieving instant view from Redis messages#705
hugefiver merged 5 commits into
devfrom
copilot/add-mcp-tool-for-instant-view

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 25, 2026

  • Create built-in MCP tool for retrieving instant view (URL preview) from Redis-stored messages
    • Added chat/internal_tools.go with the get_instant_view tool
    • Tool extracts URL entities, text links, and preview options from messages
  • Add internal tool provider pattern (separate from external mcpo HTTP tools)
    • New InternalTool struct with handler function
    • InitInternalTools(), GetInternalTool(), GetInternalToolDefinitions() functions
  • Implement get_instant_view tool that retrieves URL entities from messages stored in Redis
    • Parameters: chat_id, message_id
    • Returns: URLs, text links, and preview options in JSON format
  • Register built-in tools alongside external mcpo tools
    • Modified streaming.go to check internal tools first, then external mcpo
    • Modified chat.go to include internal tools in request
    • Added UseInternalTools config option in ChatConfigSingle
    • Added InitInternalTools() call in main.go
  • Add tests for the new tool
    • Added chat/internal_tools_test.go with comprehensive tests
  • Run linting and tests - all pass
  • Address review feedback:
    • Made TestGetInternalToolDefinitions more resilient by not hardcoding count
    • Simplified Chat function by removing intermediate variable
    • Wired UseInternalTools into regeneration path for consistent behavior

Usage Example

To enable internal tools for a chat configuration:

chats:
  - name: my_chat
    use_internal_tools: true  # Enable built-in tools
    # ... other config

The AI can call get_instant_view with {"chat_id": -1001234, "message_id": 123} to retrieve URL info.

Original prompt

为chat功能增加一个mcp工具用于从redis获取消息结构中的instant view


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: hugefiver <18693500+hugefiver@users.noreply.github.com>
Copilot AI changed the title [WIP] Add MCP tool for retrieving instant view from Redis Add MCP tool for retrieving instant view from Redis messages Jan 25, 2026
Copilot AI requested a review from hugefiver January 25, 2026 20:19
@hugefiver hugefiver marked this pull request as ready for review January 26, 2026 05:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a built-in internal MCP-style tool (get_instant_view) and wiring so chat completions can call it to fetch URL/link preview information from Redis-stored Telegram messages, alongside existing external MCP tools.

Changes:

  • Introduces an internal tools framework (InternalTool) and a get_instant_view tool that extracts URL entities, text links, and PreviewOptions from messages fetched via orm.GetMessage.
  • Extends chat configuration with a use_internal_tools flag and updates the chat request/streaming pipeline to merge internal tools with external MCP tools when enabled.
  • Initializes internal tools at startup in main.go and adds unit tests covering internal tool registration, argument error handling, instant view extraction, and human-readable formatting.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
main.go Calls chat.InitInternalTools() during startup so internal tools (currently get_instant_view) are registered before any chat handling begins.
config/chat.go Adds UseInternalTools to ChatConfigSingle (with mapstructure:"use_internal_tools") for per-chat enabling of internal tools, and keeps feature settings (regeneration options) organized.
chat/chat.go Computes useInternalTools from config, builds a combined request.Tools slice from internal definitions and external MCP tool sets, and passes a unified useTools flag into newStreamProcessor so streaming can handle tool calls.
chat/streaming.go Enhances handleToolCalls to try internal tools first and then external MCP tools (when available), logs clearer error cases (missing tool vs no provider), and uses a single flag to decide whether tool calls are allowed at all.
chat/internal_tools.go Defines the internal tools abstraction, registers the get_instant_view tool with an OpenAI function schema, implements Redis-backed message lookup, URL/text-link/preview extraction, result marshaling, and a helper to format results for human display.
chat/internal_tools_test.go Adds tests for initialization and lookup of internal tools, tool definition export shape, invalid-argument handling for get_instant_view, URL/link/preview extraction behavior, and formatting of instant view results.

Comment thread chat/internal_tools_test.go Outdated
Comment on lines +66 to +70
require.NotNil(t, definitions)
require.Len(t, definitions, 1) // We have 1 internal tool

// Verify the tool definition
def := definitions[0]
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestGetInternalToolDefinitions currently asserts require.Len(t, definitions, 1), which will start failing as soon as any additional internal tools are added. To keep this test resilient while still verifying behavior, consider checking that the slice is non-empty and explicitly asserting that a tool named get_instant_view exists in the returned definitions, rather than hard-coding the exact count.

Suggested change
require.NotNil(t, definitions)
require.Len(t, definitions, 1) // We have 1 internal tool
// Verify the tool definition
def := definitions[0]
require.NotEmpty(t, definitions)
// Find the get_instant_view tool definition
idx := -1
for i, def := range definitions {
if def.Function.Name == "get_instant_view" {
idx = i
break
}
}
require.NotEqual(t, -1, idx, "get_instant_view tool definition should be present")
// Verify the get_instant_view tool definition
def := definitions[idx]

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggested change in commit 1f3ca17. The test now uses require.NotEmpty(t, definitions) and searches for the get_instant_view tool by name rather than assuming a fixed count.

Comment thread chat/chat.go Outdated
Comment on lines +341 to +342
useTools := useMcp || useInternalTools
processor := newStreamProcessor(chatCtx, ctx, placeholderMsg, useTools, &request, &messages, v2)
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here useTools := useMcp || useInternalTools is passed as the fourth argument to newStreamProcessor, whose parameter/field is still named useMcp. Since that flag now controls both internal tools and external MCP tools, the existing name is misleading; consider renaming the parameter/field to something like useTools (and updating call sites) to better reflect its broadened responsibility.

Suggested change
useTools := useMcp || useInternalTools
processor := newStreamProcessor(chatCtx, ctx, placeholderMsg, useTools, &request, &messages, v2)
processor := newStreamProcessor(chatCtx, ctx, placeholderMsg, useMcp || useInternalTools, &request, &messages, v2)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified in commit 1f3ca17 - removed the intermediate variable and inlined useMcp || useInternalTools directly in the function call.

Comment thread config/chat.go
Comment on lines +168 to +171
Features FeatureSetting `mapstructure:"features"`
UseMcpo bool `mapstructure:"use_mcpo"`
UseInternalTools bool `mapstructure:"use_internal_tools"` // Enable built-in internal tools like get_instant_view
Filters ChatFilterSetting `mapstructure:"filters"`
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseInternalTools is now exposed as a per-chat flag and is honored in the main Chat flow, but the regeneration path (regenerateResponse in chat/reaction.go) still only uses UseMcpo to populate request.Tools and to enable tool handling. This means internal tools like get_instant_view will be available on the first response but not on regenerations for the same chat; if you want consistent behavior, consider wiring UseInternalTools into the regeneration request and its newStreamProcessor call in the same way as in Chat.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 1f3ca17. Added UseInternalTools support to regenerateResponse in reaction.go - it now includes internal tools in the request and passes the combined flag to newStreamProcessor, matching the behavior in Chat.

@icceey icceey changed the base branch from master to dev January 27, 2026 06:08
@hugefiver
Copy link
Copy Markdown
Collaborator

@copilot apply changes based on the comments in this thread

…Tools into regeneration

Co-authored-by: hugefiver <18693500+hugefiver@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 30, 2026

@copilot apply changes based on the comments in this thread

All three review comments have been addressed in commit 1f3ca17:

  1. Made test more resilient by searching for tool by name instead of hardcoded count
  2. Removed intermediate useTools variable
  3. Wired UseInternalTools into regeneration path

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@hugefiver hugefiver merged commit 58105fc into dev Jan 30, 2026
15 checks passed
@hugefiver hugefiver deleted the copilot/add-mcp-tool-for-instant-view branch January 30, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants